alvas
alvas

Reputation: 122012

Extract a line if it contains a word in a specified column

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

Upvotes: 2

Views: 5204

Answers (2)

fedorqui
fedorqui

Reputation: 289505

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

Upvotes: 6

flecoq
flecoq

Reputation: 11

For this kind of needs I always use awk :

awk -F'\t' '$2 ~ /coach/ {print $0;}' < textFile

You have access to all columns with $x, $0 contains the entire line. The test is made with regexp, quite simple in this case, so it's really powerfull if your need gets more complicated.

Upvotes: 1

Related Questions