unix - Extract a line if it contains a word in a specified column -
i want extract line if contains word in specified column of textfile. how can on one-liner unix command it? maybe cat, echo, cut, grep several piples or something.
i have textfile looked format
#sentenceid<tab>sentence1<tab>sentence2<tab>other_unknown_number_of_columns<tab> ... an example of textfile looks this:
021348 english sentence coach . c'est la phrase française avec l'entraîneur . , there several nonsense columns these . 923458 english sentence without word . c'est une phrase d'une autre anglais sans le bus mot . whatever foo bar nonsense columns 2134234 $%^& the command should output if word looking coach in 2nd column:
021348 english sentence coach . c'est la phrase française avec l'entraîneur . , there several nonsense columns these . i can python such, i'm looking unix command or one-liner:
outfile = open('out.txt') line in open('in.txt'): if "coach" in line.split(): print>>outfile, line
what this?
awk -f'\t' '{if($2 ~ "coach") print} your_file -f'\t'--> makes delimiter tab.$2 ~ "coach"--> looks "coach" in second field.print $0orprint--> prints whole line.
edit
sudo_o has suggested following, shorter:
awk -f'\t' '$2~/coach/' file
Comments
Post a Comment