1832892 Members
2393 Online
110048 Solutions
New Discussion

Re: awk

 
SOLVED
Go to solution
cfeitosa
Frequent Advisor

awk

Hello guys.

Please I need a help with the awk command.

When I run the command:

:#/ls -al prof* | head -1 |awk '{print $9}'

I have the following:
prof.u18

How can I get only "u18" in the same line command?

Please, could someone give me a help?

Thanks,
clefeitosa
6 REPLIES 6
Ivan Krastev
Honored Contributor
Solution

Re: awk

Use another awk:

echo prof.u18 | awk -F "." '{print$2}'



regards,
ivan

James R. Ferguson
Acclaimed Contributor

Re: awk

Hi:

# ls -al prof* | head -1 | awk '{split($9,a,".");print a[2]}'

...although you can eliminate the 'head' process too:

# ls -al prof* | awk '{NR==1 && split($9,a,".");print a[2]}'

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: awk

Hi (again):

...and since it's the last field of the 'ls' output you want:

# # ls -al prof* | awk '{NR==1 && split($NF,a,".");print a[2]}'

...that is, why count nine fields when $NF points to the last...

Thus is you "slip" and do 'ls -als' and there are ten fields, everything still works!

Regards!

...JRF...
cfeitosa
Frequent Advisor

Re: awk

Hi,

Thanks for all answer.
Was very useful

I'm using the following:
ls -al prof* | head -1 | awk -F "." '{print $2}'

Thanks a lot
Sandman!
Honored Contributor

Re: awk

# ls -al prof* | awk -F. 'NR==1 {print $NF}'
Dennis Handly
Acclaimed Contributor

Re: awk

You of course can change your ls(1) to:
$ ls -a prof*

Then you only need to process the one field and don't have to count. :-)