Match columns and print specific field


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Match columns and print specific field
# 1  
Old 11-07-2014
Match columns and print specific field

Hello,

I have data in following format.
Code:
pxname,svname,qcur,qmax,scur,smax,slim,stot,bin,bout,dreq,dresp,ereq,econ,eresp,wretr,wredis,status,weight,act,bck,chkfail,chkdown,lastchg,downtime,qlimit,pid,iid,sid,throttle,lbtot,tracked,type,rate,rate_lim,rate_max,check_status,check_code,check_duration,hrsp_1xx,hrsp_2xx,hrsp_3xx,hrsp_4xx,hrsp_5xx,hrsp_other,hanafail,req_rate,req_rate_max,req_tot,cli_abrt,srv_abrt,

main,FRONTEND,,,0,3,15000,61,13752,14647,0,0,1,,,,,OPEN,,,,,,,,,1,1,0,,,,0,0,0,12,,,,0,60,0,1,0,0,,0,12,61,,,
apache2,elb0,0,0,0,1,,3,687,723,,0,,0,0,0,0,UP,1,1,0,0,0,45,0,,1,2,1,,3,,2,0,,1,L4OK,,0,0,3,0,0,0,0,0,,,,0,0,
apache2,elb1,0,0,0,1,,3,687,723,,0,,0,0,0,0,UP,1,1,0,0,0,45,0,,1,2,2,,3,,2,0,,1,L4OK,,0,0,3,0,0,0,0,0,,,,0,0,
apache2,elb2,0,0,0,1,,3,687,723,,0,,0,0,0,0,UP,1,1,0,0,0,45,0,,1,2,3,,3,,2,0,,1,L4OK,,0,0,3,0,0,0,0,0,,,,0,0,
apache2,elb3,0,0,0,1,,3,687,723,,0,,0,0,0,0,UP,1,1,0,0,0,45,0,,1,2,4,,3,,2,0,,1,L4OK,,0,0,3,0,0,0,0,0,,,,0,0,
apache2,elb4,0,0,0,1,,3,687,723,,0,,0,0,0,0,UP,1,1,0,0,0,45,0,,1,2,5,,3,,2,0,,1,L4OK,,0,0,3,0,0,0,0,0,,,,0,0,
apache2,BACKEND,0,0,0,3,0,60,13752,14460,0,0,,0,0,0,0,UP,24,24,0,,0,45,0,,1,2,0,,60,,1,0,,12,,,,0,60,0,0,0,0,,,,,0,0,

Now I want to print "slim" value for "main,FRONTEND" or "ereq" value for "apache2,elb4" or process data in similar fashion. I tried below command but it prints entire column.
Code:
#awk -F, '/main,FRONTEND/{print}' socat.txt

it prints entire line which has around 51 fields, among that one value carries "slim" field value.I dont know field number of "slim". I want to print "slim" value of "main,FRONTEND" column. Giving field number to awk can work but since there are lots of fields and elbs I dont want to use that option.

Thanks,
Pushpraj

Last edited by Franklin52; 11-07-2014 at 06:59 AM.. Reason: Please use code tags
# 2  
Old 11-07-2014
Code:
awk -F ',' 'NR == 1{for(i=1; i<=NF; i++) a[$i] = i; next}
 /^main,FRONTEND/ {print $(a["slim"])}
 /^apache2,elb4/ {print $(a["ereq"])}' socat.txt

This User Gave Thanks to SriniShoo For This Post:
# 3  
Old 11-07-2014
Thanks Srini, Its working fine Smilie
I have one query, What needs to be done if I have to send matching strings as a variable to same awk command. My variable would be like mentioned below
Code:
proxy="main,FRONTEND"
field="slim"

These variable will be set in shell script and awk will refer it accordingly.
How do I integrate these variable into the same command which you have provided.

Thanks Again,

Pushpraj

Last edited by Franklin52; 11-07-2014 at 06:59 AM.. Reason: Please use code tags
# 4  
Old 11-07-2014
Code:
awk -F ',' 'NR == 1{for(i=1; i<=NF; i++) a[$i] = i; next}
 $0 ~ "^" proxy {print $(a[field])}' proxy="main,FRONTEND" field="slim" socat.txt

This User Gave Thanks to SriniShoo For This Post:
# 5  
Old 11-07-2014
Thanks, code is working fine. Now I am facing another issue with exact string match.
if my variable value is "apache2,elb1" and the file contains lines like "apache2,elb1" "apache2,elb11" "apache2,elb12"
In this case code fetching values for all the 3 lines rather than just "apache2,elb1"

Thanks,
Pushpraj
# 6  
Old 11-07-2014
Please use code tags as required by forum rules!

Try (untested): $0 ~ "^" proxy FS {...
This User Gave Thanks to RudiC For This Post:
# 7  
Old 11-07-2014
Thanks, It worked Smilie Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to print text in field if match and range is met

In the awk below I am trying to match the value in $4 of file1 with the split value from $4 in file2. I store the value of $4 in file1 in A and the split value (using the _ for the split) in array. I then strore the value in $2 as min, the value in $3 as max, and the value in $1 as chr. If A is... (6 Replies)
Discussion started by: cmccabe
6 Replies

2. Shell Programming and Scripting

Command/script to match a field and print the next field of each line in a file.

Hello, I have a text file in the below format: Source Destination State Lag Status CQA02W2K12pl:D:\CAQA ... (10 Replies)
Discussion started by: pocodot
10 Replies

3. UNIX for Advanced & Expert Users

Match and print based on columns

HI, I have 2 different questions in this thread. Consider 2 files as input (input file have different line count ) File 1 1 1 625 56 1 12 657 34 1 9 25 45 1 2 20 54 67 3 25 35 27 4 45 73 36 5 125 56 45 File2 1 1 878 76 1 9 83 67 2 20 73 78 4 47 22 17 3 25 67 99 (4 Replies)
Discussion started by: rossi
4 Replies

4. Shell Programming and Scripting

How to print with awk specific field different from specific character?

Hello, i need help with awk. I have this file: cat number DirB port 67 er_enc_out 0 er_bad_os 0 DirB port 71 er_enc_out 56 er_bad_os 0 DirB port 74 er_enc_out 0 er_bad_os 0 DirB port 75 ... (4 Replies)
Discussion started by: elilmal
4 Replies

5. Shell Programming and Scripting

Match two columns from two files and print output

Hello, I have two files which are of the following format File 1 which has two columns Protein_ID Substitution NP_997239 T53R NP_060668 V267M NP_058515 P856A NP_001206 T55M NP_006601 D371Y ... (2 Replies)
Discussion started by: nans
2 Replies

6. UNIX for Dummies Questions & Answers

How to match on phrase at beginning of line and specific columns?

Here is my file: 700 7912345678910 61234567891234567891 700 8012345678910 61234567891234567891 I want to pull all lines that begin with '700' only if columns 11-12 are '79'. My code so far only pulls the '79', not the whole line: grep ^700 file1 | cut -c 11,12 |... (7 Replies)
Discussion started by: Scottie1954
7 Replies

7. UNIX for Dummies Questions & Answers

Match columns and write specific word

Hi all I have another question as of now. I have two files One file contain data like this Serendipity glamerus Shenpurity In another file these entries are present in different columns like this from 2 column onwards SRN Serendipity Non serendipity ... (1 Reply)
Discussion started by: Priyanka Chopra
1 Replies

8. Shell Programming and Scripting

Match and print columns in second file

Hi All, I have to match each row in file 1 with 1st row in file 2 and print the corresponding column from file2. I am trying to use an awk script to do this. For example cat File1 X1 X3 X4 cat File2 ID X1 X2 X3 X4 A 1 6 2 1 B 2 7 3 3 C 3 8 4 1 D 4 9 1 1 (3 Replies)
Discussion started by: newpro
3 Replies

9. Shell Programming and Scripting

Script to Match first field of two Files and print

Hi, I want to get script or command in Sun Unix which matches first fields of both the files and print the feilds of one files, example may make it more clear. InputFile1 ================== Alex,1,fgh Menthos,45454,toto Gothica,855,ee Zenie4,77,gg Salvatore,66,oo Dhin,1234,papapa... (3 Replies)
Discussion started by: indian.ace
3 Replies

10. Shell Programming and Scripting

Print last occurrence if first field match

Hi All, I have an input below. If the term in the 1st column is equal, print the last row which 1st column is equal.In the below example, it's " 0001 k= 27 " and " 0004 k= 6 " (depicted in bold). Those terms in 1st column which are not repetitive are to be printed as well. Can any body help me... (9 Replies)
Discussion started by: Raynon
9 Replies
Login or Register to Ask a Question