LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 06-04-2010, 08:31 AM   #1
twchambers
LQ Newbie
 
Registered: Jun 2010
Posts: 10

Rep: Reputation: 0
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.
 
Old 06-04-2010, 11:19 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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!

Last edited by colucix; 06-04-2010 at 11:20 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
SED - remove last four characters from string 3saul Linux - Software 12 01-16-2023 10:21 AM
advance sed to remove multile lines. ufmale Linux - Newbie 8 05-14-2010 09:44 AM
sed to remove specific lines in a file tekmann33 Linux - Newbie 3 05-21-2009 03:41 PM
unable to insert string on particular lines using sed livetoday Linux - Newbie 5 03-30-2008 11:28 PM
Remove string in sed twantrd Programming 7 09-13-2006 02:28 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 05:58 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration