Find the answer to your Linux question:
Results 1 to 3 of 3
Hey all, I have this two-column set of data: Example.txt APP LHYYG APP LHTBG APP LHFDG APP LHSDF I need awk to print "OK" everytime the first column contains APP ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Feb 2013
    Posts
    37

    Help with awk


    Hey all,

    I have this two-column set of data:

    Example.txt
    APP LHYYG
    APP LHTBG
    APP LHFDG
    APP LHSDF


    I need awk to print "OK" everytime the first column contains APP and the second anything starting LH.

    This statement makes the work fine:
    awk '{if ( $1 ~ /APP/ && $2 ~ /^LH/) print "OK"}' Example.txt

    Allright, even easy for a newby like me.

    However,

    if the data looks like this, where APP only appears in the first row:

    APP LHYYG
    LHTBG
    LHFDG
    LHSDF

    How do I tell awk to pretend APP is also present in the next rows then print "OK" as above (ie, everytime the first column contains APP and the second anything starting LH) ?

    Any input?

  2. #2
    Linux Newbie
    Join Date
    Nov 2012
    Posts
    237
    hi,

    Code:
    awk '{if ( (NF==1 && $1 ~ "^LH") || ($1 == "APP" && $2 ~ "^LH") ) print "OK"}' Example.txt

  3. #3
    Just Joined!
    Join Date
    Feb 2013
    Posts
    37
    Quote Originally Posted by watael View Post
    hi,

    Code:
    awk '{if ( (NF==1 && $1 ~ "^LH") || ($1 == "APP" && $2 ~ "^LH") ) print "OK"}' Example.txt
    Excellent, thanks watael!

  4. $spacer_open
    $spacer_close

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •