LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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-02-2013, 02:24 PM   #1
sumncguy
Member
 
Registered: Jun 2013
Posts: 71

Rep: Reputation: Disabled
Sed: Not working as expected. Search out one line, print next line


Test Data file called testfile
Quote:
voice-port 0/0/0
caller-id enable
voice-port 0/0/1
caller-id enable
voice-port 0/0/2
voice-port 0/0/3
caller-id enable
voice-port 1/0/0
voice-port 1/0/1
voice-port 1/0/2
voice-port 1/0/3
voice-port 1/0/14
voice-port 1/0/15

Code:
sed -n '/^voice\-port /{ N; / caller/p; }' testfile
Output

Quote:
voice-port 0/0/0
caller-id enable
voice-port 0/0/1
caller-id enable
What Im after - voice port line, followed by caller id line.
Quote:
voice-port 0/0/0
caller-id enable
voice-port 0/0/1
caller-id enable
voice-port 0/0/3
caller-id enable
Its clear to me that the lines "voice followed by voice" that do not follow the pattern are creating my problem, but Im not sure how to get around that. I have the sed pocket guide and searched the web but having found anything that would help.

Thanks in advance ...
 
Old 10-02-2013, 02:33 PM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981
What about this?
Code:
sed -n '/^voice-port /{N; /caller/!D; p}' testfile
the !D command causes the deletion of the content of the pattern space every time the next line doesn't match "caller" therefore it solves the voice followed by voice issue.
 
1 members found this post helpful.
Old 10-02-2013, 03:21 PM   #3
sumncguy
Member
 
Registered: Jun 2013
Posts: 71

Original Poster
Rep: Reputation: Disabled
Thanks colucix .. that worked ... was racking my brain on that one. Hate it when I get stuck !!

Thanks again !!
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Using sed to search and stop at a blank line Tech109 Programming 25 03-19-2013 12:43 AM
[SOLVED] Insert line using sed or awk at line using line number as variable sunilsagar Programming 11 02-03-2012 10:48 AM
[SOLVED] php search for string and print line gcclinux Programming 4 08-02-2011 02:32 AM
[SOLVED] Sed - print line number along with the line ? ntpntp Linux - Newbie 6 01-30-2011 05:58 AM
sed multi-line search/replace woes djmm Programming 8 03-17-2009 05:25 AM

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

All times are GMT -5. The time now is 11:53 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration