How to grep nth word in line?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to grep nth word in line?
# 1  
Old 11-13-2013
How to grep nth word in line?

my input file content is like this
Code:
GEFITINIB 403 14 -4.786873 -4.786873 -1.990111 0.000000 0.000000 -1.146266 -39.955912 483
VANDETANIB 404 21 -4.754243 -4.754243 -2.554131 -0.090303 0.000000 -0.244210 -41.615502 193
VANDETANIB 405 21 -4.737541 -4.737541 -2.670195 -0.006006 0.000000 -0.285579 -40.793145 472
PELITINIB 406 20 -4.697441 -4.697441 -1.154811 -0.323963 0.000000 -0.238975 -37.666605 934 
Neratinib 40 19 -4.680369 -4.680369 -1.618439 -0.196470 0.000000 -0.001702 -51.090346 405
1m17-co 408 1 -4.643390 -4.643390 -1.279303 -0.455054 0.000000 -0.899078 -39.117333 429
1m17-co 409 1 -4.634879 -4.634879 -1.465411 -0.430470 0.000000 -0.900950 -38.233987 405

my output should be line whose second word is 405
Code:
VANDETANIB 405 21 -4.737541 -4.737541 -2.670195 -0.006006 0.000000 -0.285579 -40.793145 472

If I use grep command
Code:
grep 405 file

it gives three lines
Code:
VANDETANIB 405 21 -4.737541 -4.737541 -2.670195 -0.006006 0.000000 -0.285579 -40.793145 472
Neratinib 40 19 -4.680369 -4.680369 -1.618439 -0.196470 0.000000 -0.001702 -51.090346 405
1m17-co 409 1 -4.634879 -4.634879 -1.465411 -0.430470 0.000000 -0.900950 -38.233987 405

but I want only the line whose second word is 405

Thanks in advance

Last edited by Franklin52; 11-13-2013 at 08:32 AM.. Reason: Please use code tags
# 2  
Old 11-13-2013
Code:
awk '$2==405' inputfile

Is probably the simplest way to do this. Using regular expressions which you would need for grep are a little more complex.
# 3  
Old 11-13-2013
Its working
Thank you very much
# 4  
Old 11-13-2013
Hello,

One more approach, may help. Let us say check_second_field file have the input data.

Code:
awk -F" " -vs1="405" '{
        v=$2;
        if(v==s1)
        print $0 }' check_second_field

Output will be as follows.

Code:
VANDETANIB 405 21 -4.737541 -4.737541 -2.670195 -0.006006 0.000000 -0.285579 -40.793145 472


Thanks,
R. Singh
# 5  
Old 11-13-2013
Quote:
Originally Posted by jim mcnamara
Code:
awk '$2==405' inputfile

Is probably the simplest way to do this. Using regular expressions which you would need for grep are a little more complex.
Thanks its working

---------- Post updated at 06:36 PM ---------- Previous update was at 06:35 PM ----------

Thank you very much
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Get the nth word of mth line in a file

Hi.. May be a simple question but I just began to write unix scripts a week ago, for sorting some huge amount of experiment data, so I got no common sense about unix scripting and really need your helps... The situation is, I want to read the nth word of mth line in a file, and then store it... (3 Replies)
Discussion started by: freezelty
3 Replies

2. Shell Programming and Scripting

Calculating average for every Nth line in the Nth column

Is there an awk script that can easily perform the following operation? I have a data file that is in the format of 1944-12,5.6 1945-01,9.8 1945-02,6.7 1945-03,9.3 1945-04,5.9 1945-05,0.7 1945-06,0.0 1945-07,0.0 1945-08,0.0 1945-09,0.0 1945-10,0.2 1945-11,10.5 1945-12,22.3... (3 Replies)
Discussion started by: ncwxpanther
3 Replies

3. Shell Programming and Scripting

how to find third(nth) word in all line from a file

For example i'm having the below contents in a file: expr is great when you want to split a string into just two parts. The .* also makes expr good for skipping a variable number of words when you don't know how many words a string will have. But expr is lousy for getting, say, the fourth word... (2 Replies)
Discussion started by: bangarukannan
2 Replies

4. Shell Programming and Scripting

Read last word of nth line

Hi people; i want to read the last word of the 14th line of my file1.txt. Here is the EXACT 14th line of the file. 250 SectorPortnum=3,AuxPortInUngo=2,PortDeviceGroup=1,PortDeviceSet=1,PorDevice=1 20 >>> Set. i have to get the word Set. how can i call it and also how... (3 Replies)
Discussion started by: gc_sw
3 Replies

5. UNIX for Dummies Questions & Answers

How to grep word + next line in solaris 10?

Hi my unix friends, How do I grep a word and next line with solaris 10. tnx Mehrdad ---------- Post updated at 03:17 PM ---------- Previous update was at 03:09 PM ---------- I found this one: cat <file_name> | awk '/<seek_word>/ {print;getline;print}' (3 Replies)
Discussion started by: mehrdad68
3 Replies

6. Shell Programming and Scripting

grep a word instead of a line

Hi all, I'm a noobie at shell scripting (and Linux) with no experience on programing. I'm trying to create a script that will download my xml calendar from Google, process it in the format and dump it into $HOME/.calendar/calendar. Eg: April 10 Event I'm having a hard time using... (4 Replies)
Discussion started by: victorbrca
4 Replies

7. Shell Programming and Scripting

grep a word from a specific line

for example: searches only for single word for single word this is line three match=$(grep -n -e "single" data.txt) this command will stored "..... single ...... single" into search. how can i grep the single word just from line 2 only?? (3 Replies)
Discussion started by: blurboy
3 Replies

8. UNIX for Dummies Questions & Answers

grep a word from a line

Hi there, Is there a way to grep a word from a line and display only the words beginning from the search word? Say I have a file named file.txt that contains below: I am a file Hello there Reader Orbix I hope you feel good today What I want is to grep the word "there": $ grep "there"... (2 Replies)
Discussion started by: Orbix
2 Replies

9. Shell Programming and Scripting

print a line containing word in a column using grep

hi, how to print a row which contains a perticular word in its third column using grep, cut, or any thing else. thanks (2 Replies)
Discussion started by: useless79
2 Replies

10. Shell Programming and Scripting

Can a shell script pull the first word (or nth word) off each line of a text file?

Greetings. I am struggling with a shell script to make my life simpler, with a number of practical ways in which it could be used. I want to take a standard text file, and pull the 'n'th word from each line such as the first word from a text file. I'm struggling to see how each line can be... (5 Replies)
Discussion started by: tricky
5 Replies
Login or Register to Ask a Question