Get first word only starting of a line

Hi,

I am trying to get longest line in a .gz file without extract the file.
I am using the below command to get the longest line.

zcat /a/b/c/file.gz |awk '{print length, $0}'|sort -nr|head -1
247 AAAAAAAAAAAAAAAAAAAAAAAAA BBBBBBBBBBBBBBBBBBBAAAAAA CCCCCCCCCCC DDDDDDDDDDDD EEEEEEEEEEEEEE FFFFFFFFFFFFFF GGGGGGGGG HHHHHHHHHHHHHHHH IIIIIIIIIIIIII JJJJJJJJJJJJJJJ KKKKKKKKKKKKK LLLLLLLLLL MMMMMMMM NNNNNNNNNNNNN XXXXXXXXXXXXXX ZZZZZZZZZZZZZZZZZA

From the output i want to get 247 only. I wish to add extra command like cut commdn to add existing above command. Thanks in advance

Feel free to add a cut . Where is the problem?

Got it. I added the below command and I got the first word of the output.

cut -d ' ' -f1

Perfect :slight_smile:

If you're using awk anyway, why not do it all in awk ?

zcat /a/b/c/file.gz |awk 'length > m{m = length}END{print m}'

gets rid of the need for sort , head , and cut .

2 Likes