regex - How to grep values under columns

08
2014-07
  • user327963

    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%  
    
  • Answers
  • fedorqui

    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%
    

  • Related Question

    regex - Regular expression and grep not working
  • Wuffers

    I have the following regular expression:

    ([:digit:]{4})-([:digit:]{1,2})-([:digit:]{1,2})
    

    It should get dates in this format:

    2010-12-19
    

    And I am using it on filenames that look like this:

    2010-12-19-xxx-xxx-xxx.markdown
    

    And, when I use it with grep like this:

    echo $POST | grep -oE "([:digit:]{4})-([:digit:]{1,2})-([:digit:]{1,2})" # $POST is the filename
    

    It doesn't work, I just get emptiness.


  • Related Answers
  • Andy Smith

    Try this:-

    echo $POST | grep -oE "[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}"
    

    If I try it here, I get:-

    [andys@daedalus ~]$ echo "2010-12-19-aaa-bbb-ccc-ddd.markdown" | grep -oE "[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}"
    2010-12-19
    

    Hope that's what you're looking for.

  • frabjous

    Andy's answer is fine, but if you want something closer to your original syntax, you could try:

    echo $POST | egrep -oE "([[:digit:]]{4})-([[:digit:]]{1,2})-([[:digit:]]{1,2})"

    You need egrep here for extended regular expressions, and the double brackets for character classes.

  • Dennis Williamson

    You don't need the parentheses, but you do need more square brackets. Character classes have the same characteristics as individual characters. Just as you might search for vowels like this: [aeiou], or digits like this: [0123456789] or this: [0-9], you need to enclose a class such as [:digit:] or [:upper:] in a bracket expression as well: [[:xdigit:]] (hex digits).

    grep -oE "[[:digit:]]{4}-[[:digit:]]{1,2}-[[:digit:]]{1,2}"