LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Using sed to remove lines around a specified string (https://www.linuxquestions.org/questions/linux-general-1/using-sed-to-remove-lines-around-a-specified-string-812136/)

twchambers 06-04-2010 08:31 AM

Using sed to remove lines around a specified string
 
Hi there,
I have a data set that takes the form...

0.0 43
12572.9102 80.8521 263.3575 0.0200 12.6358 -86.4942
4.3870e-06 -0.3547
0.0 44
0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
0.0000e+00 0.2518
0.0 01
12127.0830 84.8586 255.2578 0.0200 10.2361 -85.3434
3.5641e-07 0.3923

I want to be able to identify the line with all zeros and remove it and the lines above and below.

I have found that its relatively easy to remove this line and the one below using sed, but am struggling to find a way of removing the one above it as it cannot be uniquely identified.

Thanks in advance, Tom.

colucix 06-04-2010 11:19 AM

You can play with the 'x' command to exchange the hold and the pattern space. Provided that the following will print a blank line (which is in the hold space at start-up) followed by all the lines except the last one (which is retained in the hold space and never printed out):
Code:

sed -n 'x;p' file
you have to insert two rules to manage the first and the last line:
Code:

sed -n 'x;1d;p;${x;p}' file
this deletes the first line (which is the blank line line exchanged with the hold buffer) and print out the last one, since ${x;p} matches the end of the file and exchanges again the pattern space with the hold buffer, so that the last line is released and printed out. This actually prints out the entire file, but every time it parses a new input line, it retains the previous one in the hold buffer.

Now we have to establish a rule to manage the matching line. I would use an extended regexp and the following commands:
Code:

sed -rn '/(0\.0000 *){6}/{n;n;x;d};x;1d;p;${x;p}' file
where it reads in the next two lines, then it exchanges the hold buffer and delete the previously stored line (this solves your struggle). Now the hold buffer contains the second line after the match. It will be printed out at the next pass.

Hope it's clear. Not easy to explain, but I assumed you had a look at http://www.grymoire.com/Unix/Sed.html... a must-read! :)


All times are GMT -5. The time now is 11:27 AM.