LinuxQuestions.org
Review your favorite Linux distribution.
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-14-2011, 03:38 PM   #1
porphyry5
Member
 
Registered: Jul 2010
Location: oregon usa
Distribution: Slackware 14.1, Arch, Lubuntu 18.04 OpenSUSE Leap 15.x
Posts: 518

Rep: Reputation: 24
sed print range from pattern1 to pattern2


From a file I want to extract a range of lines by patterns. I've used variations on
Code:
sed -n -e '/^BashNotes/,/^EndOf[A-Za-z]*$/ p' -e '/^EndOf[A-Za-z]*$/ q' Notes
So, I want to extract lines starting from one whose first word is specified, in this case "BashNotes", and ending at the first line consisting of the single word "EndOf...", which in this case would be "EndOfBashNotes".

Either I get no output at all, or it prints from the start of file to the first EndOf..., so the problem has to be with "^BashNotes", e.g. remove the "^" and it accesses an earlier occurrence of "BashNotes" that is in the middle of the first line of the file, and prints to the first occurrence of "EndOf...".

So why should a "^" in the "from" pattern be objectionable, when it is acceptable in the "to" pattern and the "quit" statement?
 
Old 06-14-2011, 07:31 PM   #2
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Quote:
Originally Posted by porphyry5 View Post
From a file I want to extract a range of lines by patterns. I've used variations on
Code:
sed -n -e '/^BashNotes/,/^EndOf[A-Za-z]*$/ p' -e '/^EndOf[A-Za-z]*$/ q' Notes
So, I want to extract lines starting from one whose first word is specified, in this case "BashNotes", and ending at the first line consisting of the single word "EndOf...", which in this case would be "EndOfBashNotes".

Either I get no output at all, or it prints from the start of file to the first EndOf..., so the problem has to be with "^BashNotes", e.g. remove the "^" and it accesses an earlier occurrence of "BashNotes" that is in the middle of the first line of the file, and prints to the first occurrence of "EndOf...".

So why should a "^" in the "from" pattern be objectionable, when it is acceptable in the "to" pattern and the "quit" statement?
Hi,

without some sample data it is hard to guess why it does not work. Maybe some leading spaces? Try
Code:
sed -n -e '/^[[:blank:]]*BashNotes/ ...'
If this does not fix the issue then provide a representative sample file.
 
1 members found this post helpful.
Old 06-14-2011, 09:35 PM   #3
porphyry5
Member
 
Registered: Jul 2010
Location: oregon usa
Distribution: Slackware 14.1, Arch, Lubuntu 18.04 OpenSUSE Leap 15.x
Posts: 518

Original Poster
Rep: Reputation: 24
Quote:
Originally Posted by crts View Post
Hi,

without some sample data it is hard to guess why it does not work. Maybe some leading spaces? Try
Code:
sed -n -e '/^[[:blank:]]*BashNotes/ ...'
If this does not fix the issue then provide a representative sample file.
Has the same result, nothing is returned. There is no leading whitespace. I apologize, I should have mentioned in the first post that my regexes work exactly as expected in a different text editor, kwrite as it happens, but I was thinking I had to have made some syntactical error with sed. I've attached the front end of the file concerned.
Attached Files
File Type: txt Notes.txt (154.2 KB, 15 views)
 
Old 06-14-2011, 10:38 PM   #4
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Ok, I see now. Try the following to see the error:
Code:
$ sed -n -e '/^EndOf[A-Za-z]*$/ p' -e '/^EndOf[A-Za-z]*$/ q' Notes
EndOfAwkCommands
As you can see, your RegEx matches 'EnOfAwkCommands' and therefore it quits. Since there 'BashNotes' has not been encountered before - when you use the 'sed' in your initial post - nothing will get printed.
This worked for me:
Code:
sed -n '/^BashNotes/,/^EndOf[A-Za-z]*$/ p' Notes
It printed 624 lines. I am not sure why you want to use the 'q' command.
 
1 members found this post helpful.
Old 06-15-2011, 04:47 AM   #5
Reuti
Senior Member
 
Registered: Dec 2004
Location: Marburg, Germany
Distribution: openSUSE 15.2
Posts: 1,339

Rep: Reputation: 260Reputation: 260Reputation: 260
Depending on the size maybe the q command can save some time (and/or avoid to find any further block of BashNotes/EndOf...).
 
Old 06-15-2011, 08:48 AM   #6
porphyry5
Member
 
Registered: Jul 2010
Location: oregon usa
Distribution: Slackware 14.1, Arch, Lubuntu 18.04 OpenSUSE Leap 15.x
Posts: 518

Original Poster
Rep: Reputation: 24
Quote:
Originally Posted by crts View Post
Ok, I see now. Try the following to see the error:
Code:
$ sed -n -e '/^EndOf[A-Za-z]*$/ p' -e '/^EndOf[A-Za-z]*$/ q' Notes
EndOfAwkCommands
As you can see, your RegEx matches 'EnOfAwkCommands' and therefore it quits. Since there 'BashNotes' has not been encountered before - when you use the 'sed' in your initial post - nothing will get printed.
This worked for me:
Code:
sed -n '/^BashNotes/,/^EndOf[A-Za-z]*$/ p' Notes
It printed 624 lines. I am not sure why you want to use the 'q' command.
Thank you very much. You mean that the quit statement gets a match before it ever gets to BashNotes. I was under the impression that the commands proceeded stepwise, that the range match would have to complete before it ever tried the quit test. That was only there to stop it processing any further once it satisfied the range command.
 
Old 06-15-2011, 09:06 AM   #7
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Quote:
Originally Posted by porphyry5 View Post
You mean that the quit statement gets a match before it ever gets to BashNotes.
Yes, that is exactly what happens. If for some reason you want to quit processing after the range has been printed then you can do the following:
Code:
sed -n '/^BashNotes/,/^EndOf[A-Za-z]*/ {p;/^EndOf[A-Za-z]*/q}' Notes
This will execute the 'q' command only when inside the range. Notice, that I removed the 'end of line' marker '$' to accommodate for trailing spaces.
 
1 members found this post helpful.
Old 06-15-2011, 09:57 AM   #8
porphyry5
Member
 
Registered: Jul 2010
Location: oregon usa
Distribution: Slackware 14.1, Arch, Lubuntu 18.04 OpenSUSE Leap 15.x
Posts: 518

Original Poster
Rep: Reputation: 24
Quote:
Originally Posted by crts View Post
Yes, that is exactly what happens. If for some reason you want to quit processing after the range has been printed then you can do the following:
Code:
sed -n '/^BashNotes/,/^EndOf[A-Za-z]*/ {p;/^EndOf[A-Za-z]*/q}' Notes
This will execute the 'q' command only when inside the range. Notice, that I removed the 'end of line' marker '$' to accommodate for trailing spaces.
Thank you again, so one uses {...} to ensure the previous command is satisfied before accessing the commands in the braces. No harm is done by removing either ^ or $, I only use those compound word arrangements for titles and end markers, to distinguish them from regular text, so any range combination can only occur once.
 
  


Reply



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] SED and Replacing Specific occurrence or Range of Lines bridrod Linux - Newbie 7 08-27-2009 09:59 AM
[SOLVED] sed print range to end of file schneidz Programming 4 04-08-2009 01:30 PM
Delete a line range with sed J_Szucs Programming 8 04-29-2008 11:43 PM
sed: delete text till <pattern2> depending on length of text oyarsamoh Programming 2 05-05-2007 01:40 AM
Sed - replace text only in a certain range twantrd Programming 4 11-30-2006 06:54 PM

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

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