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 06-16-2011, 05:45 PM   #1
Euler2
LQ Newbie
 
Registered: Oct 2009
Posts: 20

Rep: Reputation: 0
Sed -n '/pattern/{n;p;}' is missing matches in file


I have an interesting problem, sed '/pattern/{n;p;}' file, doesn't seem to be catching all of the matches in my file. As an example of this see below.

test:

Turn
Turn
Turn
H310
Turn
Turn
Alpha
Coil
Turn
Turn

sed -n '/Turn/{n;p}' test
output:
Turn
H310
Turn
Turn

But what I expect is:

Turn
Turn
H310
Turn
Alpha
Turn

Any ideas what's going wrong and how to fix it?

(my thought is that the pattern is being matched, sed moves to the next line, prints, but then doesn't bother to check if there is a match)
 
Old 06-16-2011, 07:59 PM   #2
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
Try sample data which adds line numbers. Then it will be easier to debug. Usually the `-n' option is used with pattern matches and the `p' command.
 
Old 06-16-2011, 08:26 PM   #3
Euler2
LQ Newbie
 
Registered: Oct 2009
Posts: 20

Original Poster
Rep: Reputation: 0
Following your suggestion, I went back and added line numbers, now the output is:

2 Turn
4 H310
6 Turn
10 Turn

while I expect:

2 Turn
3 Turn
4 H310
6 Turn
7 Alpha
10 Turn

Which matches what I thought was happening, that sed is identifying matches, advancing to the next line and printing, but after advancing is not checking to see if the line it advanced to matches the pattern. Any ideas on how to get my desired result?
 
Old 06-16-2011, 09:57 PM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Not sure on getting the desired result but you are aware that the n;p says get the next line and print it.
Sed cannot then say I need to check the next line as well (not as written).

Now I am guessing one of the gurus will have this a better way, but the following seems to work:
Code:
sed -n 'x;/Turn/{x;p;x}' file
 
Old 06-17-2011, 05:57 AM   #5
Reuti
Senior Member
 
Registered: Dec 2004
Location: Marburg, Germany
Distribution: openSUSE 15.2
Posts: 1,339

Rep: Reputation: 260Reputation: 260Reputation: 260
What about a loop in case of a match:
Code:
$ sed -n ": start;/Turn/{n;p;b start}" test
 
Old 06-17-2011, 07:28 AM   #6
Euler2
LQ Newbie
 
Registered: Oct 2009
Posts: 20

Original Poster
Rep: Reputation: 0
The loop worked like a charm, thanks! (Solved!)
 
  


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
Shell Script to Delete part text of a line if pattern matches harsha1980 Programming 36 04-16-2010 03:36 AM
sed: delete lines after last occurrence of a pattern in a file zugvogel Programming 4 11-17-2009 01:49 AM
Sed to read the file and replace and insert the pattern saurabhchokshi Programming 2 06-12-2009 01:15 PM
printing pattern match and not whole line that matches pattern Avatar33 Programming 13 05-06-2009 06:17 AM
how to use the sed w option to redirect pattern match to file nickleus Linux - General 11 04-18-2006 08:34 AM

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

All times are GMT -5. The time now is 07:16 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