Display lines before and after string until blank line
Hello,
I have some date formatted in this way: Code:
line of text - display lines before STRING until blank line and include it - display lines after STRING until 2nd blank line and include it The result I'm after is this: Code:
|
Try this:
Code:
awk 'BEGIN{ FS="\n" ; RS="\n\n" ; x = "0" } /STRING/ { x = $0 ; next } x !~ /0/ { printf("\n%s\n\n%s\n", x, $0) ; x="0" }' input Code:
$ awk 'BEGIN{ FS="\n" ; RS="\n\n" ; x = "0" }/STRING/ { x = $0 ; next }x !~ /0/ { printf("\n%s\n\n%s\n", x, $0) ; x="0" }' input |
Hi druuna, thank you for your help.
I just tried your command on the example I provided here and it worked. But when I tried the command on my test file, it doesn't display anything. I suspect the command doesn't work on my actual data stream maybe because the blank lines I'm seeing are not really blank lines? I am actually trying to pipe tcpdump output to your awk command. In the output I see those blank lines and if I copy the output and paste it into a file and read it with vi, I can still see the blank lines. But when I view the file in Notepad for example, the blank lines are gone and everything is a compact block of text. Maybe I should specify a certain parameter in tcpdump in order to inject real blank lines between each packet output. Any thoughts? |
Upon further testing I found that it may have something to do with the output of the data stream, not the blank lines. I am testing a simplified version and adding to it from my original data stream in order to find at what point the awk command stops working.
|
The input used is important and without the actual output it is hard to help you.
You mention tcpdump, can you post the exact command used and an example of the actual output generated? Do use code tags to preserve the layout. |
Found the issue. Will create another post so that druuna will be alerted by incoming new post.
|
I just found what causes awk to stop displaying the results. Whenever a "0" is present in any of the lines: line with the STRING, line before STRING, line after STRING, awk will stop printing the whole block of lines between "before 1st blank line" and "after 2nd blank line" conditions. So, if I modify my original example by adding a 0 at any lines:
Code:
line of text Code:
x = "0" I must confess it is very hard for me to read awk syntax even if I know what it does in this case. |
Troubleshooting without answers to the questions I asked in post #5 will be hard.
Without knowing any details, try this: Code:
awk 'BEGIN{ FS="\n" ; RS="\n\n" ; x = "_Token" } /STRING/ { x = $0 ; next } x !~ /_Token/ { printf("\n%s\n\n%s\n", x, $0) ; x="_Token" }' input Code:
$ cat input |
BTW: Here's a breakdown of the awk command:
Code:
BEGIN{ FS="\n" ; RS="\n\n" ; x = "_Token" } I also fill x with a unique token, the 0 used earlier isn't unique. Code:
/STRING/ { x = $0 ; next } Code:
x !~ /_Token/ { printf("\n%s\n\n%s\n", x, $0) ; x="_Token" } |
Thank you so much, it works now.
Sorry I didn't provide the actual output but I thought I should save you from doing all the work and see why awk did not print the lines and get back directly with the answer. Now there is one more tiny issue: it seems that if there is a 2nd blank line right after the 1st one, your awk command doesn't see it and will continue to print the lines after the blank double-lines. This happens only for the lines after STRING, it seems that the command doesn't count the 2nd blank line if it is right after the 1st. |
I feel we could be over thinking this one a little:
Code:
awk '/STRING/{printf $0 RS;getline;print}' RS="\n\n" file |
@grail, nice and small but it doesn't address the double blank lines (which, as you mentioned, is handled correct in your and my previous code).
Based on my previous replies: Code:
awk ' |
Thank you both for your input.
Is there a way to address the double blank lines issue? Tried with grail's command, the result is the same, it only works as intended if there are no double blank lines. |
How about taking what you have been given and trying to come up with a solution to your new problem.
Current solutions solve for initial problem so it should not take a big change to overcome the new requirement I would add that my version seemed to work fine by placing a second empty line after the record with STRING in it |
Quote:
Input Code:
line of text Code:
line of text |
All times are GMT -5. The time now is 06:14 AM. |