1826639 Members
3348 Online
109695 Solutions
New Discussion

Print in awk

 
M. Tariq Ayub
Regular Advisor

Print in awk

Hi,

I want to print the last colum using awk. the column number may very

drwxrwxr-x 2 tariq users 1024 Jun 27 13:36 .
drwxr-xr-x 5 tariq users 1024 Jun 27 12:11 ..
-rw-rw-r-- 1 tariq users 5 Jun 27 13:36 test
w-rw-r-- 1 tariq users 6 Jun 10 09:38 file01
-rw-rw-r-- 1 tariq users 7 Jun 10 09:38 file02
-rw-rw-r-- 1 tariq users 7 Jun 10 09:39 file03

from the above putput i want to print only
test
file01
file02
file03

How can i do that. I cannot use print $4 as sometime the file name will be in different column.
8 REPLIES 8
Con O'Kelly
Honored Contributor

Re: Print in awk

Hi

Sorry if I'm misunderstanding you, but can you not use:
$ ls -l | awk '{print $9}' | grep -v "^.[?]"

OR alternatively use:
$ ls -l | awk '{print $NF}' | grep -v "^.[?]"

Cheers
Con
Muthukumar_5
Honored Contributor

Re: Print in awk

You can get last column with NF parameter as,

ls -l | awk '{ print $NF }'

hth.
Easy to suggest when don't know about the problem!
Cem Tugrul
Esteemed Contributor

Re: Print in awk

Hi,
another solution but similiar as others
from me;
ls -lrt|grep -v "^d"|awk '{print $NF}'
Good Luck,
Our greatest duty in this life is to help others. And please, if you can't
Cem Tugrul
Esteemed Contributor

Re: Print in awk

"^d" anchor directory so "grep -v exclude
directories so you can get only files...
good luck,
Our greatest duty in this life is to help others. And please, if you can't
Peter Nikitka
Honored Contributor

Re: Print in awk

Hi,

if you really want to list files only, try

ls -p | awk '!/[@/]'

It's fast and you need not bother with filenames containing spaces.

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Muthukumar_5
Honored Contributor

Re: Print in awk

You can also try with perl as,


# ls -l | perl -ne 'split(/ /); $var=@_; $var-=1;print $_[$var];'

hth.
Easy to suggest when don't know about the problem!
Brian Butscher
Frequent Advisor

Re: Print in awk

If all you want to do is print the file names, why not use "ls |lp -dprintername"? No need to use awk.

Regards,

Brian
Sandman!
Honored Contributor

Re: Print in awk

Why use awk or sed when you can do it simply with the ls command:

# ls -1

cheers!