Get first word only starting of a line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Get first word only starting of a line
# 1  
Old 04-26-2016
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.

Code:
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
# 2  
Old 04-26-2016
Feel free to add a cut. Where is the problem?
# 3  
Old 04-26-2016
Got it. I added the below command and I got the first word of the output.

HTML Code:
cut -d ' ' -f1
# 4  
Old 04-26-2016
Perfect Smilie
# 5  
Old 04-26-2016
If you're using awk anyway, why not do it all in awk?
Code:
zcat /a/b/c/file.gz |awk 'length > m{m = length}END{print m}'

gets rid of the need for sort, head, and cut.
These 2 Users Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

How to grep a line not starting with # from a file (there are two lines starting with # and normal)?

e.g. File name: File.txt cat File.txt Result: #INBOUND_QUEUE=FAQ1 INBOUND_QUEUE=FAQ2 I want to get the value for one which is not commented out. Thanks, (3 Replies)
Discussion started by: Tanu
3 Replies

2. Shell Programming and Scripting

Find word in a line and output in which line the word occurs / no. of times it occurred

I have a file: file.txt, which contains the following data in it. This is a file, my name is Karl, what is this process, karl is karl junior, file is a test file, file's name is file.txt My name is not Karl, my name is Karl Joey What is your name? Do you know your name and... (3 Replies)
Discussion started by: anuragpgtgerman
3 Replies

3. Shell Programming and Scripting

Read a File line by line and split into array word by word

Hi All, Hope you guys had a wonderful weekend I have a scenario where in which I have to read a file line by line and check for few words before redirecting to a file I have searched the forum but,either those answers dint work (perhaps because of my wrong under standing of how IFS... (6 Replies)
Discussion started by: Kingcobra
6 Replies

4. Shell Programming and Scripting

Deleting rows starting with a character and word

Hi, I have multiple files of same format and I want to delete the lines starting with # and The from all of them I am using egrep -v '^(#|$)' for # but unable to do for both # and The Please guide Thanks (12 Replies)
Discussion started by: bioinfo
12 Replies

5. Shell Programming and Scripting

Replace a word in a string starting with another word

Hi All, I have a file in which a number of lines are starting with similar first word but different next words. I want to replace the any nth word(not 1st or 2nd) with another word. Eg:- My file contains are like this:- Ram is a boy. Ram is a good boy. Ram plays cricket. Here I want to... (2 Replies)
Discussion started by: mukeshbaranwal
2 Replies

6. Shell Programming and Scripting

Search the word to be deleted and delete lines above this word starting from P1 to P3

Hi, I have to search a word in a text file and then I have to delete lines above from the word searched . For eg suppose the file is like this: Records P1 10,23423432 ,77:1 ,234:2 P2 10,9089004 ,77:1 ,234:2 ,87:123 ,9898:2 P3 456456 P1 :123,456456546 P2 abc:324234 (2 Replies)
Discussion started by: vsachan
2 Replies

7. Shell Programming and Scripting

Using awk to print line starting with particular word

Hi Geeks, Consider this line: driver=c:\folder1\folder2 The above line is contained in a variable say 'var' . I want to copy everything after 'driver=' in to another variable say var2. Please tell me how can this be done. (8 Replies)
Discussion started by: ajincoep
8 Replies

8. Shell Programming and Scripting

How to remove all words starting from a matching word in a line

Hi Guys, I have a file like this: wwwe 1 ioie ewew yyy uuu 88 erehrlk 4 ihoiwhe lkjhassad lkhsad yyy mmm 45 jhash lhasdhs lkhsdkjsn ouiyrshroi oihoihswodnw oiyhewe yyy ggg 77 I want to remove everything after "yyy" and including "yyy" from each line in the file. So I want:... (2 Replies)
Discussion started by: npatwardhan
2 Replies

9. Shell Programming and Scripting

Print word 1 in line 1 and word 2 in line 2 if it matches a pattern

i have a file in this pattern MATCH1 word1 IMAGE word3 word4 MATCH2 word1 word2 word3 word4 MATCH2 word1 word2 word3 word4 MATCH2 word1 word2 word3 word4 MATCH2 word1 word2 word3 word4 MATCH1 word1 IMAGE word3 word4 MATCH2 word1 word2 word3 word4 MATCH2 word1 word2 word3 word4 MATCH2 word1... (7 Replies)
Discussion started by: bangaram
7 Replies

10. Shell Programming and Scripting

need a cmd to search starting word

example - shsk mss-sdsd-asd i need a command which will search for staring word not others it should search only -shsk cat filename | grep '-' will search whole '-' in the file but i need to search only staring '-' thank u revenna (1 Reply)
Discussion started by: revenna
1 Replies
Login or Register to Ask a Question