SED/AWK - Delete all lines until empty line is found after pattern match
Dear All,
I am sitting on a CentOS 5.4 server and trying to delete all lines in a file until a blank line is found after a string is matched. For E.g. I have file 123 Code:
$ cat 123 Code:
hi Code:
$ sed -n '/hello/,/^$/p' 123 Any suggestions ? Thanks in advance. |
Hi All,
I have found a partial solution. I redirected the file and use the awk one liner but it DOES NOT preserves blank lines :scratch: Code:
$ sed -n '/hello/,/^$/p' 123 > 456 |
How about:
Code:
awk '/hello/{while(getline && $0 != ""){}}1' 123 |
Hi.
Code:
$ sed '/hello/,/^$/{/^$/!d}' test.dat |
Quote:
Daniel B. Martin |
Fantastic
Quote:
You have been my saviour number of times. Many many thanks for this perfect one liner. :hattip: |
@Daniel - sorry about that ... forget some times as just come up with solution and move on (my bad)
Ultimately the while loop must have an action associated so a blank set of braces will execute nothing for each loop. As the loop ends when it finds a line that is empty, when it leaves the loop the 1 at the end will also print this line (which the user wanted) Please let me know if you need further information and thanks again for the reminder ;) |
Quote:
Code:
awk '/hello/{while(getline && $0 != ""){}}1' 123 My Dougherty & Robbins "sed & awk" book doesn't tell about getline. In context, it reads the next line. How and why is the getline and-ed with the logical result of $0 != ""? Daniel B. Martin |
Hi, Daniel.
Quote from `info gawk getline': Quote:
|
Quote:
Please consider this input file which is the same as that posted by OP but for two lines added at the end. Code:
how Daniel B. Martin |
Quote:
|
Quote:
1) Start suppressing printing whenever you see hello. 2) Stop suppressing printing whenever you see a blank line. There was no assurance that each and every hello had a blank line "partner." This is another grain of evidence which shows that writing an airtight spec is more difficult than it might seem. Daniel B. Martin |
Quote:
Quote:
The other point to remember is that any programming is only ever as good as the information supplied, hence based on example data and information the current script is a worthy solution. Of course anything could be added to the data to cause anyone one solution to fail. |
Quote:
Daniel B. Martin |
All times are GMT -5. The time now is 01:55 PM. |