Printing entire field, if at least one row is matching by AWK


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Printing entire field, if at least one row is matching by AWK
# 1  
Old 10-07-2011
Printing entire field, if at least one row is matching by AWK

Dear all,
I have been trying to print an entire field, if the first line of the field is matching.
For example, my input looks something like this.
Code:
aaa      ddd       zzz
123      987       126
24        0.650    985
354      9864     0.32
0.333   4324      000

I am looking for a pattern, for example "ddd", which is in the second field and first row. Then I need to print the entire field, output should look like below.
Code:
ddd
987
0.650
9864
4324

How can I solve this by awk or sed?
Your help is appreciated.

Last edited by radoulov; 10-07-2011 at 08:06 AM.. Reason: Code tags.
# 2  
Old 10-07-2011
I imagine there are much neater ways, but this seems to work:
Code:
 awk -vMYCOL="ddd" '{if (NR == 1) { for (i=1;i<=NF;i++) { if ($i == MYCOL) { mycolnum = i } } } { print $mycolnum }}' file1

EDIT: Changed a bit to output the header line.

Also, if there are no matching columns it just prints everything - if you want no output you'd need an additional if:
Code:
 awk -vMYCOL="ddd" '{if (NR == 1) { for (i=1;i<=NF;i++) { if ($i == MYCOL) { mycolnum = i } } } { if (mycolnum) { print $mycolnum }}}' file1


Last edited by CarloM; 10-07-2011 at 08:02 AM.. Reason: Output the header
This User Gave Thanks to CarloM For This Post:
# 3  
Old 10-07-2011
try this,
Code:
awk '{for(i=1;i<=NF;i++) {if(NR==1 && $i~/ddd/) {print $i;a=i;next}if(a>0){print $a;next}}}'   filename

This User Gave Thanks to pravin27 For This Post:
# 4  
Old 10-07-2011
If Perl is acceptable (this should handle multiple matching columns):

Code:
perl -lane'
  $. == 1 and @f = grep $F[$_] =~ /ddd/, 0..@F;
  @f and print join "\t", @F[@f]
  ' infile

# 5  
Old 10-09-2011
Code:
pattern=ddd
awk -v p=$pattern 'NR==1{for(i=1;i<=NF;i++) if ($i~p) c=i}{print $c}' infile

# 6  
Old 10-09-2011
Code:
awk -v var=ddd 'NR==1{for(i=0;++i<=NF;)if($i==var){_=i}}{$0=$_}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

Awk: count unique elements in a field and sum their occurence across the entire file

Hi, Sure it's an easy one, but it drives me insane. input ("|" separated): 1|A,B,C,A 2|A,D,D 3|A,B,B I would like to count the occurence of each capital letters in $2 across the entire file, knowing that duplicates in each record count as 1. I am trying to get this output... (5 Replies)
Discussion started by: beca123456
5 Replies

2. UNIX for Beginners Questions & Answers

Continued trouble matching fields in different files and selective field printing ([g]awk)

I apologize in advance, but I continue to have trouble searching for matches between two files and then printing portions of each to output in awk and would very much appreciate some help. I have data as follows: File1 PS012,002 PRQ 0 1 1 17 1 0 -1 3 2 1 2 -1 ... (7 Replies)
Discussion started by: jvoot
7 Replies

3. Shell Programming and Scripting

awk to update field using matching value in file1 and substring in field in file2

In the awk below I am trying to set/update the value of $14 in file2 in bold, using the matching NM_ in $12 or $9 in file2 with the NM_ in $2 of file1. The lengths of $9 and $12 can be variable but what is consistent is the start pattern will always be NM_ and the end pattern is always ;... (2 Replies)
Discussion started by: cmccabe
2 Replies

4. Shell Programming and Scripting

Comparing same column from two files, printing whole row with matching values

First I'd like to apologize if I opened a thread which is already open somewhere. I did a bit of searching but could quite find what I was looking for, so I will try to explaing what I need. I'm writing a script on our server, got to a point where I have two files with results. Example: File1... (6 Replies)
Discussion started by: mitabrev83
6 Replies

5. Shell Programming and Scripting

awk find the first matching row

I would like to find the first matching row and continue to search from the matching row. if column3 equal to 1 in the row, find the next matching row based on the criteria below. column3 in the matching row equal to 0 and column2 equal to column2 in the matching row test.csv - test data ... (7 Replies)
Discussion started by: chailee
7 Replies

6. UNIX for Dummies Questions & Answers

Using awk to find max and printing entire line

Hi folks, I am very new to awk. I have what is probably a very simple question. I'm trying to get the max value of column 1, but also print column 2. My data looks like this: 0.044|2000-02-03 14:00:00 5.23|2000-02-03 05:45:00 5.26|2000-02-03 11:15:00 0|2000-02-01 18:30:00 So in this case... (2 Replies)
Discussion started by: amandarobe
2 Replies

7. Shell Programming and Scripting

Matching and printing line with awk

Hi there, I'm trying to use awk to print out the entire line that contains a match to a certain regex and then append some text,plus the match to the end of the line. So far I have: awk -F: '{print "RG:Z:" $2}' file Which prints out the match I want plus the additional text, but I'm stuck... (3 Replies)
Discussion started by: jim_lad
3 Replies

8. Shell Programming and Scripting

awk if statement not printing entire field

I have an input that looks like this: chr1 mm9_knownGene utr3 3204563 3206102 0 - . gene_id "Xkr4"; transcript_id "uc007aeu.1"; chr1 mm9_knownGene utr3 4280927 4283061 0 - . gene_id "Rp1"; transcript_id "uc007aew.1"; chr1 mm9_knownGene ... (5 Replies)
Discussion started by: pbluescript
5 Replies

9. Shell Programming and Scripting

Help With AWK Matching and Re-printing Lines

Hi All, I'm looking to use AWK to pattern match lines in XML file - Example patten for below sample would be /^<apple>/ The sample I wrote out is very basic compared to what I am actually working with but it will get me started I would like to keep the matched line(s) unchanged but have them... (4 Replies)
Discussion started by: rhoderidge
4 Replies

10. Shell Programming and Scripting

awk transpose row into 2 field column

Need to transpose every 2 fields of a row into a single 2 field column. input 4 135 114 76 217 30 346 110 5 185 115 45 218 85 347 125 6 85 116 130 220 65 352 95 11 30 117 55 221 42 355 75 16 72 118 55 224 37 357 430 17 30 119 55 225 40 358 62 21 52 120 65 232 480 360 180 ....... (8 Replies)
Discussion started by: sdf
8 Replies
Login or Register to Ask a Question