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. |
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 Code:
sed -n 'x;1d;p;${x;p}' file 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 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. |