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

    Join Date
    May 2012
    Posts
    14
    Rep Power
    0

    Regarding awk start and end pattern


    Hi,

    In a file I am trying to find a pattern in this way.
    File.txt

    Line 1...
    Line 2...
    ...
    ...
    Start Pattern
    ...
    ...
    XXXXEnd Pattern
    pattern line 1...
    pattern line 2...
    YYYYEnd Pattern
    ...
    ...
    ...
    ZZZZEnd Pattern
    ...
    ...

    EOF

    So using `awk '/Start Pattern/,/End Pattern/' File.txt` I can get the following:
    XXXXEnd Pattern
    pattern line 1...
    pattern line 2...
    YYYYEnd Pattern
    Now I want the End pattern line only. Meaning, in this case YYYYYEnd Pattern and NOT the ZZZEnd Pattern line/s.

    I tried `awk '/Start Pattern/,/End Pattern/' File.txt` | awk -F"\n" '{print $4}' or `awk '/Start Pattern/,/End Pattern/' File.txt` | awk -F"\r" '{print $4}'
    But I receive nothing. The reason behind, the whole return string is not separated by LF or CR. I am really surprised.
    Well, when I tried this
    [color=blue]`awk '/Start Pattern/,/End Pattern/' File.txt` | awk -F"\n" '{print $1}'
    Output is:
    XXXXEnd Pattern
    pattern line 1...
    pattern line 2...
    YYYYEnd Pattern
    Which means awk is returning the whole string without \n\r or what?
    Need an explanation please.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,451
    Rep Power
    1751
    Your examples given the file contents shown do not, to my tired eyes, make sense.
    Your first awk command outputs 4 lines, that last one of which is the line you want? If so, just pipe that output into a tail -1?
    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
    May 2012
    Posts
    14
    Rep Power
    0

    To process each occurance one by one...


    Hi Simon,
    Thanks for your reply.
    That really helped.

    But I forgot to mention one important thing here.

    The start/end patterns are repetitive in the file.
    So, I want to process this pattern occurrence one by one.

    eg.
    line1..
    line2..
    # First pattern
    start pattern
    ...
    ...
    end pattern
    ....
    ....
    # Second pattern
    start pattern
    ...
    ...
    end pattern
    ...
    ...
    # Third pattern
    start pattern
    ...
    ...
    end pattern
    ...
    line n...



    When I execute `awk '/start pattern/,/end pattern/' file.txt` I get
    start pattern
    ...
    ...
    end pattern
    start pattern
    ...
    ...
    end pattern
    start pattern
    ...
    ...
    end pattern

    Now, how can I process each occurrence one by one?
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,451
    Rep Power
    1751
    Ok, you're going to have to do this more 'progammatically' ...
    Off the top of my head:

    Code:
    awk 'BEGIN { start=0; }
      /start pattern/ { start=1; }
      /end pattern/ { if (start == 1) { print; start=0; } }' 
      file.txt
    Should work for you ... at awk start, set a flag variable to 0, then when you find a 'start pattern' set it to 1 to indicate it's been found.
    Also search for 'end pattern' and if we have already found the start pattern this is the line we want, so print it, and set the flag variable back to 0.

    Comments on this post

    • sujoydc agrees : Thank you very much !
    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

IMN logo majestic logo threadwatch logo seochat tools logo