How to grep values under columns

0

0

How can I grep for a specific column name and display the value underneath that column.

Sample data:

StandByFile  StandByPg  StandByLSN         StandByRcvBufUsed
S0082160.LOG 621668     0x00000C85118BC72D 0%  

I want to display 0% under StandByRcvBufUsed column. Mind you that file contains other data different from the above displayed columns, meaning there are different column lists in files on different rows.

Thanks

Update:
My file has the following format, so the column #s are not fixed:

HADR Information:
Role    State                SyncMode   HeartBeatsMissed   LogGapRunAvg (bytes)
Standby Peer                 Async    0                  365000              

ConnectStatus ConnectTime                           Timeout   
Connected     Tue May 20 09:34:23 2014 (1400592863) 120       

ReplayOnlyWindowStatus ReplayOnlyWindowStartTime             MaintenanceTxCount
Inactive               N/A                                   0         


PrimaryFile  PrimaryPg  PrimaryLSN        
S0082160.LOG 621668     0x00000C85118BC72D

StandByFile  StandByPg  StandByLSN         StandByRcvBufUsed
S0082160.LOG 621668     0x00000C85118BC72D 0%  

user327963

Posted 2014-05-30T14:00:13.773

Reputation: 1

Answers

0

Let's look for the number of column that contains StandByRcvBufUsed and store it. Then, jump to the next line and print that column and finally exit:

$ awk 'f{print $f; exit} {for (i=1; i<=NF; i++) if ($i == "StandByRcvBufUsed") {f=i; next}}' file
0%

fedorqui

Posted 2014-05-30T14:00:13.773

Reputation: 1 517

but my file has the following format:

ConnectStatus ConnectTime Timeout
Connected Tue May 20 09:34:23 2014 (1400592863) 120

                            0         

PrimaryFile PrimaryPg PrimaryLSN
S0082160.LOG 621668 0x00000C85118BC72D

StandByFile StandByPg StandByLSN StandByRcvBufUsed S0082160.LOG 621668 0x00000C85118BC72D 0% – user327963 – 2014-05-30T14:08:54.353

1Please update your question with this information, in comments it is quite difficult to see code. – fedorqui – 2014-05-30T14:11:22.753

So given this file, what is your exactly required output, @user327963 ? – fedorqui – 2014-05-30T14:24:15.937

I would like to see the value under StandByRcvBufUsed which would be 0% in this case.Thanks – user327963 – 2014-05-30T14:25:49.840

Ok, @user327963 see my updated answer. Now it makes it. – fedorqui – 2014-05-30T14:31:17.743

it does not work for me. I get nothing: – user327963 – 2014-05-30T14:36:19.400

awk 'f{print $f; exit} {for (i=1; i<=NF; i++) if ($i =="StandByRcvBufUsed") {f=i; next}}' 79742.7TD.000.Pmr79742DRhadr.out – user327963 – 2014-05-30T14:36:37.150

@user327963 it works fine with my awk: mawk 1.3.3 Nov 1996. Cross check if you are writing the fields properly. – fedorqui – 2014-05-30T14:38:39.737

what about if we use grep to search for column name and pipe it to awk to print its value? – user327963 – 2014-05-30T14:45:46.873

i have it worked as follows: – user327963 – 2014-05-30T15:00:20.467

cat <file> |head -24|tail -1| awk '{ print $4 }' 0% – user327963 – 2014-05-30T15:00:35.617

Nice to read that, @user327963 . However, you ended up using the col number ($4) and line number (head -24). Anyway, good that you found your desired result. – fedorqui – 2014-05-30T15:02:03.667

yes thanks!!! it would be good to have a flexible way to do this instead of knowing the line # :) – user327963 – 2014-05-30T15:03:20.873

@user327963 well this is what I tried with my solution: get the column number and then print that column on the next line. – fedorqui – 2014-05-30T15:05:12.587