LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Sed: Not working as expected. Search out one line, print next line (https://www.linuxquestions.org/questions/programming-9/sed-not-working-as-expected-search-out-one-line-print-next-line-4175479370/)

sumncguy 10-02-2013 02:24 PM

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 ...

colucix 10-02-2013 02:33 PM

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.

sumncguy 10-02-2013 03:21 PM

Thanks colucix .. that worked ... was racking my brain on that one. Hate it when I get stuck !!

Thanks again !!


All times are GMT -5. The time now is 09:49 PM.