Operating System - HP-UX
1833996 Members
2890 Online
110063 Solutions
New Discussion

want to extract field from output of cmd ls -lrt

 
SOLVED
Go to solution
diwa
Frequent Advisor

want to extract field from output of cmd ls -lrt

Hi,

cmd ls -lrt *.vix gives out put like:

-rwxrwxr-x 1 symology symology 81920 Sep 19 09:00 STF.vix

Can i extract 5th field(no of bytes) from this
output. Basically i want to take this field in some var and through loop i want to count?

Thanks
6 REPLIES 6
MarkSyder
Honored Contributor

Re: want to extract field from output of cmd ls -lrt

ls -lrt|awk '{print $5}'

Mark Syder (like the drink but spelt different)
The triumph of evil requires only that good men do nothing
Laurent Menase
Honored Contributor

Re: want to extract field from output of cmd ls -lrt

ls -lrt *.vix | while read a b c d e f
do
echo $f
done

or
set -- "$(ls -lrt *.vix)"
echo $5

or
a="$(ls ls -lrt *.vix )"
b="${a#* * * * }"
b="${b%% *}"

Oviwan
Honored Contributor
Solution

Re: want to extract field from output of cmd ls -lrt

Hey

you can also count with awk e.g. for MB:

ls -lrt *.vix | | awk '{sum+=$5} END { print sum/1024/1024 " MB"}'

Regards
Laurent Menase
Honored Contributor

Re: want to extract field from output of cmd ls -lrt

Just found I just shift e and f:

typeset -i g=0
ls -lrt | while read a b c d e f
do
echo $e $g
g=$e+g
done

echo $g
Bill Hassell
Honored Contributor

Re: want to extract field from output of cmd ls -lrt

And if you want to sort the files by size:

up:
ls -l | sort -rnk5

down:
ls -l | sort -nk5


Bill Hassell, sysadmin
diwa
Frequent Advisor

Re: want to extract field from output of cmd ls -lrt

Hi All,

Thanks a lot i got it.It is working fine.

Warm regard's