LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-23-2014, 11:36 PM   #1
ulto
Member
 
Registered: Apr 2004
Posts: 52

Rep: Reputation: 1
Sed issue with finding a match on two sequential lines and deleting the previous line


Say I have log file

cat file.log
Local server1
Local server2
Local server3
lucy
bill
Local server4
Local server5
lucy
john
Local server 6

I want Sed to output the following
Local server3
lucy
bill
Local server5
lucy
john

The logic is as follows. I want all the lines that start with Local deleted, EXCEPT if the lines after it does NOT start with LOCAL.

I came close with this.
sed -n '/Local/{x;d;};1h;1!{x;p;};${x;p;}' file.log
Local server3
lucy
Local server5
lucy

Note that it deletes the names john and bill because it deletes the line previous to the pattern match.
 
Old 10-24-2014, 08:01 AM   #2
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Must the solution use sed?

An awk solution may run faster and be more readable.

With this InFile ...
Code:
Local server1
Local server2
Local server3
lucy
bill
Local server4
Local server5
lucy
john
Local server 6
... this awk ...
Code:
awk 'FNR==1 {getline nextline <FILENAME}
            {getline nextline <FILENAME; thisline=$0;
  if (substr(thisline,1,5)!="Local" ||
      substr(nextline,1,5)!="Local") print thisline}'  \
 $InFile >$OutFile
... produced this OutFile ...
Code:
Local server3
lucy
bill
Local server5
lucy
john
Daniel B. Martin
 
1 members found this post helpful.
Old 10-24-2014, 11:37 AM   #3
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 428Reputation: 428Reputation: 428Reputation: 428Reputation: 428
Hi.

Code:
$ cat /tmp/in
Local server1
Local server2
Local server3
lucy
bill
Local server4
Local server5
lucy
john
Local server 6
$ sed -n '1!{ H; g; /^Local.*\nLocal/!P; s/[^\n]*\n//}; h' /tmp/in
Local server3
lucy
bill
Local server5
lucy
john
This will not print last line of the file and it is not clear from your description what to do in this case. Append '${/^Local/!p}' if you want to print last line if it does not start with 'Local'.

Last edited by firstfire; 10-24-2014 at 11:45 AM.
 
2 members found this post helpful.
Old 10-26-2014, 06:26 PM   #4
ulto
Member
 
Registered: Apr 2004
Posts: 52

Original Poster
Rep: Reputation: 1
Thank you, I found another way to achieve the end goal but thank you for your help.
 
  


Reply

Tags
sed



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
sed - line processing depending on previous line match BeachHead Linux - Newbie 2 06-19-2014 09:27 PM
sed multiple lines previous line gushnik1 Linux - Software 2 10-09-2013 03:15 AM
[SOLVED] SED/AWK - Delete all lines until empty line is found after pattern match vikas027 Programming 13 03-28-2012 08:33 AM
[SOLVED] sed: Match one line, make a substitution a few lines down? ShadowCat8 Programming 6 06-08-2011 07:59 PM
grep/sed/awk - find match, then match on next line gctaylor1 Programming 3 07-11-2007 08:55 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:41 PM.

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