S7_0
S7_0

Reputation: 1183

grep - how to display another word instead of the matching of grep

Given input like:

ID            VALUE
technique     lol
technology    case 
london        knife
ocean         sky

I'm currently using

grep -Eo '^[^ ]+' FILE | grep "tech"

for match every word which contain "tech" in the ID column. In this case, it display :

 technique 
 technology

However does anyone can tell me how can I display the word from the second column regarding the word matching in the first column ? For example how to display the word:

 lol
 case 

(display the value instead the key)

Also, how can I display the key (as above) and the value separate by "=" like ? (without any spaces):

key=value

Thanks

Upvotes: 0

Views: 46

Answers (2)

Benjamin W.
Benjamin W.

Reputation: 52181

You can grep for lines starting with "tech" and then just display the second column. The exact format depends on how your input file columns are separated. If they are tab separated:

grep '^tech' FILE | cut -f 2

If they are space separated:

grep '^tech' FILE | tr -s ' ' $'\t' | cut -f 2

This "squeezes" repeated spaces and replaces them with a single tab character.

For your second question, you can use

sed -n '/^tech/ s/[[:space:]]\+/=/p' FILE

This means "don't print (-n); on lines matching ^tech, make the substitution and print".

Upvotes: 3

John Hascall
John Hascall

Reputation: 9416

Using awk:

awk '$1 ~ "tech" {print $2}' < inputfile

or with key=value

awk '$1 ~ "tech" {print $1"="$2}' < inputfile

Upvotes: 1

Related Questions