Extract a line if it contains a word in a specified column
extract, grep, multiple-columns, text-files, unix
Solution
What about this?
awk -F'\t' '{if($2 ~ "coach") print} your_file
- `-F'\t'` --> makes the delimiter to be the tab.
- `$2 ~ "coach"` --> looks for "coach" in the second field.
- `print $0` or `print` --> prints the whole line.
Edit
sudo_O has suggested the following, which is even shorter:
awk -F'\t' '$2~/coach/' file
Problem
I want to extract a line if it contains a word in a specified column of a textfile. How can i do it on a one-liner unix command to do it? Maybe with `cat`, `echo`, `cut`, `grep` with several piples or something. I have a textfile that looked with this format ``` #SentenceID<tab>Sentence1<tab>Sentence2<tab>Other_unknown_number_of_columns<tab> ... ``` An example of the textfile looks like this: ``` 021348 this is the english sentence with coach . c'est la phrase française avec l'entraîneur . And then there are several nonsense columns like these . 923458 this is a another english sentence without the word . c'est une phrase d'une autre anglais sans le bus mot . whatever foo bar nonsense columns 2134234 $%^& ``` The command should output if the word i am looking for is `coach` in the 2nd column: ``` 021348 this is the english sentence with coach . c'est la phrase française avec l'entraîneur . And then there are several nonsense columns like these . ``` I can do it with python as such, but i'm looking for a unix command or something one-liner: ``` outfile = open('out.txt') for line in open('in.txt'): if "coach" in line.split(): print>>outfile, line ```