JPNagarajan
JPNagarajan

Reputation: 928

How to get a column value based on adjacent column value in linux?

I want to fetch a column value based on the next column value.i'm grepping for a column having value host and want to print the previous column value

tried using grep -Po ".* (?=host)" but didn't get the proper output

file test.log contains below sample data(all in a single row)

test Plus 193310 68FAD575EC59C2C6 exa4dbadm03  host                     


cat test.log|grep -i 193310|grep -i host|grep -Po ".* (?=host)"

I'm trying to grep the column which having value as host and print the previous column value. In this case i want to get exa4dbadm03 as a output

expected result: exa4dbadm03

Upvotes: 0

Views: 96

Answers (1)

oguz ismail
oguz ismail

Reputation: 50795

Why don't you use for this? E.g:

awk '{for(i=2;i<=NF;++i){if($i=="host"){print $(i-1);break}}}' file

Upvotes: 2

Related Questions