#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2001
    Posts
    10
    Rep Power
    0

    Awk script Help maybe advanced or maybe not


    Does anyone know how I can acheive the following using awk....

    I want to be able to match a pattern on a line and print all consecutive lines which don't have a similar pattern. Once I find a line that has the pattern I will either print the line if the pattern is an exact match or skip the line if it is not an exact match.

    For example.....

    if I have a file with these contents:

    @@abc123: line # one
    line # two
    line # three
    @@def123: line # four
    line # five
    @@abc123: line # six
    Currently I can only print the line which contains the pattern using the following Awk command....

    awk '/@@abc123:/{print}'

    I'm not sure if this is possible but I would expect the output of the awk script results to be printed as follows....

    @@abc123: line # one
    line # two
    line # three
    @@abc123: line # six
    Thanks in advance! Please let me know if this is possible and how it can be implemented.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,436
    Rep Power
    1688
    Your suggested output does not match your stated requirements!
    If you want awk to do whatever it is you wish you will need to use a varaible to indicate if you have found your pattern (does the pattern only ever appear at the beginnng of the line?) and then have a condition checking for the pattern and processing (print line or not) depending on if it's a pattern line or not and if a pattern has been previously found.
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2001
    Posts
    10
    Rep Power
    0
    Simon,
    The Pattern does only match at the beginning of the line. I guess I should have used the start of line character in the awk command I posted....

    awk '/^@@abc123:/{print}' temp.txt
    It also makes sense as to your desciption of how I should use variables to check current and previous conditions in an awk script.

    I'm pretty new to Awk and I'm not familliar with with how this could be done. I'm Just assuming that I would also have to some how use the keyword next, and match function to accomplish this. But I may be wrong.

    Would you be able to provide any insight or examples on how this could be done?
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,436
    Rep Power
    1688
    Doing just a check for the pattern will not help you. I don't have access to a *nix box atm (too lazy to spark up my netbook!), but in pseudo-code it'd be:

    Code:
    set found = 0
    while not EOF
    {
      if pattern matched
      {
         if found = 0 {
    # First time of seeing pattern, so 'remember' and print line
           set found = 1
           print line
         }
      else
     {
        if found = 1 {
    # not the patter, but we have seen the pattern, so just print
          print line
        }  
      }
    }
    In awk terms that'd be like (not tested!)

    Code:
    awk '
      BEGIN {
        found = 0;
      }
      {
       if ($1 ~ "@@abc123" ) {
         if (found == 0) {
            found = 1;
            print;
         }
        } else {
          if (found == 1) {
             print;
          }
        }
      }
    ' temp.txt
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2001
    Posts
    10
    Rep Power
    0

    Thank You!!!!


    You are a life saver!!! I was able to tweak the script that you posted to get it to do what I needed.

    The script you posted compiled without issues. After tweaking I got the following script......

    Code:
    awk '
      BEGIN {
        found = 0;
      }
      {
       if ($1 ~ "^@@abc123:" ) {
            found = 1;
            print;
       } else if(match($1,"^@@-") != 0){
            found = 0;
       } else {
          if (found == 1) {
             print;
          }
        }
      }
    '  temp.txt
    Thanks again Simon!
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,436
    Rep Power
    1688
    Glad you got it working for you!
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc
  12. #7
  13. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,837
    Rep Power
    480

    simplification


    how about this program? It prints the text between the patterns. You may need a bit more to finesse the end cases.

    Code:
    $ awk '/^@@abc123:/,/^@@-/'

    Also, all awk variables default to empty string or 0, depending on how they are used. You can dispense with the
    BEGIN { found=0 } business.

IMN logo majestic logo threadwatch logo seochat tools logo