Reputation: 1193
Lets say I have a tab separated csv file as below:
a b c
d e f
g h i
Using commandline utilities, is there a way I could return the whole column that matches a required grep pattern or in the above example, I would like to return the second column for a grep of b?
Upvotes: 0
Views: 441
Reputation: 1855
You really need to give a hint how big your files are, how often you want to run this, and how many columns you have. But
Based on the observations above, I would
Upvotes: 0
Reputation: 195129
awk -F'\t' -v pat="b" 'NR==FNR{for(i=1;i<=NF;i++)if($i~pat)c[i];next}
{s="";for(i=1;i<=NF;i++)
if(i in c)s=s sprintf("%s\t", $i);
sub(/\t$/,"",s);print s}' file file
this line does the job.
pat
, and keep in column
format. pat
is regex, you can pass a shell variable to the awk linetake a look the example: (I add a b
in your 3rd column to show multiple matching case):
kent$ cat f
a b c
d e b
g h i
kent$ awk -F'\t' -v pat="b" 'NR==FNR{for(i=1;i<=NF;i++)if($i~pat)c[i];next}{s="";for(i=1;i<=NF;i++)if(i in c)s=s sprintf("%s\t", $i);sub(/\t$/,"",s);print s}' f f
b c
e b
h i
Upvotes: 1
Reputation: 289835
If there is just a matching, you can do for example this:
$ awk -v patt="b" 'FNR==NR {for (i=1;i<=NF;i++) $i~patt && col=i; next} {print $col}' file file
b
e
h
It loops twice through the file. Firstly to get the column number of the matched text. Secondly to print that specific column.
-v patt="b"
give the patternFNR==NR {for (i=1;i<=NF;i++) $i~patt && col=i; next}
on the first read, loop through fields and check if the pattern is matched. If so, store the column number in the col
var.{print $col}
print that specific col of all lines.Upvotes: 1