Search and retrieve the next Column.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search and retrieve the next Column.
# 1  
Old 07-06-2015
Search and retrieve the next Column.

Hi Guys,

I have a File like below.

HTML Code:
FTOP,1|NOME,5|SERP,3
SERP,4|FTOP,7|POSE,4
Now, i am able to separate using multiple Delimiters in Awk. So,now when i search for FTOP, i need to retrieve the next column.

In the above case,

HTML Code:
1
7
Can somebody help as how to retrieve it.

Following is the code i have achieved till now.

Code:
awk -F[,|] '/FTOP/<dont  knw what to do now>' Filename

Thanks for your help in advance.

Cheers!!
# 2  
Old 07-06-2015
Code:
awk -F'[,|]' '{for (i=1; i<=NF; i++){if($i=="FTOP"){print $(i+1);next}}}' file

---------- Post updated at 10:03 AM ---------- Previous update was at 09:49 AM ----------

Maybe a different approach.
Code:
perl -nle '/FTOP,(\d)/ and print $1' file

# 3  
Old 07-06-2015
another way

Code:
awk -F"FTOP," '/FTOP/{ split($2,a,"|"); print a[1]}' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to search a text in file and retrieve required lines following it with UNIX command?

I have requirement to search for a text in the file and retrieve required lines that is user defined with unix command. Eg: Find the text UNIX in the below file and need to return Test 8 & Test 9 Test 1 Test 2 Test 3 Test 4 UNIX Test 5 Test 6 Test 7 Test 8 Test 9 Result can... (8 Replies)
Discussion started by: Arunkumarsak4
8 Replies

2. UNIX for Dummies Questions & Answers

Search word in 3rd column and move it to next column (4th)

Hi, I have a file with +/- 13000 lines and 4 column. I need to search the 3rd column for a word that begins with "SAP-" and move/skip it to the next column (4th). Because the 3rd column need to stay empty. Thanks in advance.:) 89653 36891 OTR-60 SAP-2 89653 36892 OTR-10 SAP-2... (2 Replies)
Discussion started by: AK47
2 Replies

3. Shell Programming and Scripting

Retrieve empty value in column

hi guys, how to retrieve "empty" value in column3 and column6 in the following text $ cat myfile.txt COL1 COL2 COL3 COL4 COL5 val1 long_value_name_that_takes_space value4 val5 COL6 COL7 ... (1 Reply)
Discussion started by: kholis
1 Replies

4. Shell Programming and Scripting

search string in a file and retrieve 10 lines including string line

Hi Guys, I am trying to write a perl script to search a string "Name" in the file "FILE" and also want to create a new file and push the searched string Name line along with 10 lines following the same. can anyone of you please let me know how to go about it ? (8 Replies)
Discussion started by: sukrish
8 Replies

5. Shell Programming and Scripting

sed/awk to retrieve max year in column

I am trying to retrieve that max 'year' in a text file that is delimited by tilde (~). It is the second column and the values may be in Char format (double quoted) and have duplicate values. Please help. (4 Replies)
Discussion started by: CKT_newbie88
4 Replies

6. Shell Programming and Scripting

have to retrieve the distinct values (not duplicate) from 2nd column and display

I have a text file names test2 with 3 columns as below . We have to retrieve the distinct values (not duplicate) from 2nd column and display. I have used the below command but giving some error. NS3303 NS CRAFT LTD NS3303 NS CHIRON VACCINES LTD NS3303 NS ALLIED MEDICARE LTD NS3303 NS... (16 Replies)
Discussion started by: shirdi
16 Replies

7. Shell Programming and Scripting

Search on a particular column

Hi Guys, I like to search on a particular column for a string. This is my file 503018," 215/55HR18 BSW","Y"," ",20080204,20080204," ","T2456GH" 503019," 215/60HR17 BSW","Y"," ",20080204,20080204," ","T2456GH" 503020," 225/55R19 BSW","Y"," ",20080204,20080204," ","T2456GH" 505023,"... (1 Reply)
Discussion started by: mac4rfree
1 Replies

8. Shell Programming and Scripting

search and retrieve previous line in file

I've got a file with the following layout: #STMP FSgroup filename /filesysname1 filestatus 2 #STMP FSstatus filename /filesysname1 ratio 30 #STMP FSgroup filename ... (2 Replies)
Discussion started by: paulsew
2 Replies

9. UNIX for Dummies Questions & Answers

(cont) Retrieve line from a file based on a value in specific column

HI, Your help was great: awk -F":" '$5 ~ /^P/{print }' file I would like to know what changes need to be done to this line code, so that I can put it in a shell script and call it as the example below. example: countries that start with chacater 'P' > country P Result: ... (0 Replies)
Discussion started by: efernandes
0 Replies

10. UNIX for Dummies Questions & Answers

Retrieve line from a file based on a value in specific column

Hi, I have a file that has several values seperated by ":" 2006:John:Student:Football:Portugal:Cinema 2006:James:Engineer:Basket:Poland:Theatre 2007:Lucy:Diver:Gymnastic:England:Music 2007:Smith:Plumber:Basket:Spain:Poker I need make a filter based on the 5th field to find countries that... (1 Reply)
Discussion started by: efernandes
1 Replies
Login or Register to Ask a Question