1833776 Members
2174 Online
110063 Solutions
New Discussion

Re: awk doubt

 
SOLVED
Go to solution
Vogra
Regular Advisor

awk doubt

Hi All!
how can I get the file name without extention from the list of files?
ex. I have the name: 24145123.sc
that 24145123 is the string I need.
thanx.
We are spirits in the material world
13 REPLIES 13
S.K. Chan
Honored Contributor

Re: awk doubt

# echo 24145123.sc | awk -F. '{print $1}'
hpuxrox
Respected Contributor
Solution

Re: awk doubt


# ls | grep ".sc" | awk -F. '{print $1}'

Sukant Naik
Trusted Contributor

Re: awk doubt

grep *.sc | awk -F. '{print $1}'

Just my two cents.
-Sukant
Who dares he wins
Sachin Patel
Honored Contributor

Re: awk doubt

Another way using cut.

ls |grep *.sc |cut -d"." -f1

Sachin
Is photography a hobby or another way to spend $
MANOJ SRIVASTAVA
Honored Contributor

Re: awk doubt

Hi Claudio

there is a simpler way if that is waht you are trying to do

echo 24145123.sc | cut -c 1-8 will give you the file name w/o the externsion.


Manoj Srivastava
hpuxrox
Respected Contributor

Re: awk doubt

Claudio Lima,

Be carefull with cut -c. Cut -c does not have the ability to do padding like awk can. I think awk is your best chose for what you are trying to do.

-Yates
Rodney Hills
Honored Contributor

Re: awk doubt

If you running a shell script under "ksh", you can use the parameter substitution as follows-

withext="12332234.sc"
without=${withext%.*}
echo $without
12332234

-- Rod Hills
There be dragons...
Darrell Allen
Honored Contributor

Re: awk doubt

Hi Claudio,

Let's get a little weird...

echo sc.sc.sckjs.scsc.scs.sc | awk '{FS="\.sc$"} {print $1}'

This allows for multiple "dots" in the filename as well as multiple occurances or ".sc". Not to mention that the "$" anchors
".sc" to the end of the filename and the "\" keeps the "." from being expanded as a wildcard.

Rodney's tip works well! It's much simpler than this.

Darrell
"What, Me Worry?" - Alfred E. Neuman (Mad Magazine)
John Carr_2
Honored Contributor

Re: awk doubt

Hi

and an alternative to awk

echo 24145123.sc | sed -e 's/.sc//g'

cheers
John
James R. Ferguson
Acclaimed Contributor

Re: awk doubt

Hi:

I can't resist...Rodney's solution is the fastest, since the shell is doing the work rather than spawning another process.

Another 'awk' variation, which would be useful if you had multiple fields (columns) each with "."s might look like this:

# echo "abc.123 def.456 ghi.789"|awk '{split($3,a,".");print a[1]}'

In the above example, "ghi" (the first part of the third field) would be returned.

Regards!

...JRF...
Graham Cameron_1
Honored Contributor

Re: awk doubt

You don't need awk.
There is a command exactly for this:
basename 24145123.sc .sc
(only works if you know the extension, ie '.sc' though)

Graham
Computers make it easier to do a lot of things, but most of the things they make it easier to do don't need to be done.
Darrell Allen
Honored Contributor

Re: awk doubt

Good one Graham! I had thought about basename but obviously didn't read the man page very closely. Duh! Learned something again!

This points out how often there are other uses for UNIX commands other than the one(s) we typically use. Also the value of these forums.

N/A please.

Darrell
"What, Me Worry?" - Alfred E. Neuman (Mad Magazine)
Charles Stepp
New Member

Re: awk doubt

I like sed:

echo "this.that" | sed 's/\..*//'
Keep It Simple Sir