I am looking for an easy way to extract columns from a text file based on a regular expression, kind of like grep but searching and returning columns instead.
for example, suppose i have the following file, 'file.txt':
A B C D B
1 2 3 4 5
6 7 8 9 0
5 6 7 8 9
2 3 8 9 0
I would like a command, let's call it transpose_grep, that does this:
cat file.txt | transpose_grep 'C'
It would return:
C
3
8
7
8
To give another example:
cat file.txt | transpose_grep 'B'
would return two columns, since 'B' appears twice:
B B
2 5
7 0
6 9
3 0
Is there a unix tool I can use to do this? Thanks for the help!