Print output matching pattern till other pattern match

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
0
down vote

favorite












I am trying to print text after a pattern match till it matches other pattern several times in a file. I have tried to modify the script given here but failed to do it.
eg.
The content of file1.txt



example text
more example
pattern1
important text
very important
need this too
pattern2
i dont require this
junk text
more junk
pattern1
important text
very important
need this too
pattern2
junk


Expected Output



pattern1
important text
very important
need this too
pattern1
important text
very important
need this too


can anyone suggest the edit?



Thanks.







share|improve this question





















  • Im absolutely sure this has a duplicate, but can't find it with a cursory search.
    – Kusalananda
    Apr 20 at 10:43














up vote
0
down vote

favorite












I am trying to print text after a pattern match till it matches other pattern several times in a file. I have tried to modify the script given here but failed to do it.
eg.
The content of file1.txt



example text
more example
pattern1
important text
very important
need this too
pattern2
i dont require this
junk text
more junk
pattern1
important text
very important
need this too
pattern2
junk


Expected Output



pattern1
important text
very important
need this too
pattern1
important text
very important
need this too


can anyone suggest the edit?



Thanks.







share|improve this question





















  • Im absolutely sure this has a duplicate, but can't find it with a cursory search.
    – Kusalananda
    Apr 20 at 10:43












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I am trying to print text after a pattern match till it matches other pattern several times in a file. I have tried to modify the script given here but failed to do it.
eg.
The content of file1.txt



example text
more example
pattern1
important text
very important
need this too
pattern2
i dont require this
junk text
more junk
pattern1
important text
very important
need this too
pattern2
junk


Expected Output



pattern1
important text
very important
need this too
pattern1
important text
very important
need this too


can anyone suggest the edit?



Thanks.







share|improve this question













I am trying to print text after a pattern match till it matches other pattern several times in a file. I have tried to modify the script given here but failed to do it.
eg.
The content of file1.txt



example text
more example
pattern1
important text
very important
need this too
pattern2
i dont require this
junk text
more junk
pattern1
important text
very important
need this too
pattern2
junk


Expected Output



pattern1
important text
very important
need this too
pattern1
important text
very important
need this too


can anyone suggest the edit?



Thanks.









share|improve this question












share|improve this question




share|improve this question








edited Apr 20 at 10:40









Jeff Schaller

31.1k846105




31.1k846105









asked Apr 20 at 10:22









Pradyumna Sagar

82




82











  • Im absolutely sure this has a duplicate, but can't find it with a cursory search.
    – Kusalananda
    Apr 20 at 10:43
















  • Im absolutely sure this has a duplicate, but can't find it with a cursory search.
    – Kusalananda
    Apr 20 at 10:43















Im absolutely sure this has a duplicate, but can't find it with a cursory search.
– Kusalananda
Apr 20 at 10:43




Im absolutely sure this has a duplicate, but can't find it with a cursory search.
– Kusalananda
Apr 20 at 10:43










1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










Matching between lines between patterns, including either boundary, is a standard range selector in sed:



sed -n -e '/pattern1/,/pattern2/p' example.txt


Depending on how efficiency-critical you are (how big the files are), I'd probably be lazy and use a second pass to delete the pattern2 markers:



cat example.txt 
| sed -n -e '/pattern1/,/pattern2/p'
| sed -e '/pattern2/d'


(Yes, this is a useless use of cat, because I prefer the readability of chaining several piping filters over the performance loss, and I'm not sure right now if < example.txt | sed ... | sed ... is mandated by POSIX or just an extension that happens to be present in bash and zsh.)






share|improve this answer

















  • 1




    Also, sed -n '/pattern1/,/pattern2//pattern2/d;p;' file.
    – Kusalananda
    Apr 20 at 10:45











Your Answer







StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f438906%2fprint-output-matching-pattern-till-other-pattern-match%23new-answer', 'question_page');

);

Post as a guest






























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote



accepted










Matching between lines between patterns, including either boundary, is a standard range selector in sed:



sed -n -e '/pattern1/,/pattern2/p' example.txt


Depending on how efficiency-critical you are (how big the files are), I'd probably be lazy and use a second pass to delete the pattern2 markers:



cat example.txt 
| sed -n -e '/pattern1/,/pattern2/p'
| sed -e '/pattern2/d'


(Yes, this is a useless use of cat, because I prefer the readability of chaining several piping filters over the performance loss, and I'm not sure right now if < example.txt | sed ... | sed ... is mandated by POSIX or just an extension that happens to be present in bash and zsh.)






share|improve this answer

















  • 1




    Also, sed -n '/pattern1/,/pattern2//pattern2/d;p;' file.
    – Kusalananda
    Apr 20 at 10:45















up vote
1
down vote



accepted










Matching between lines between patterns, including either boundary, is a standard range selector in sed:



sed -n -e '/pattern1/,/pattern2/p' example.txt


Depending on how efficiency-critical you are (how big the files are), I'd probably be lazy and use a second pass to delete the pattern2 markers:



cat example.txt 
| sed -n -e '/pattern1/,/pattern2/p'
| sed -e '/pattern2/d'


(Yes, this is a useless use of cat, because I prefer the readability of chaining several piping filters over the performance loss, and I'm not sure right now if < example.txt | sed ... | sed ... is mandated by POSIX or just an extension that happens to be present in bash and zsh.)






share|improve this answer

















  • 1




    Also, sed -n '/pattern1/,/pattern2//pattern2/d;p;' file.
    – Kusalananda
    Apr 20 at 10:45













up vote
1
down vote



accepted







up vote
1
down vote



accepted






Matching between lines between patterns, including either boundary, is a standard range selector in sed:



sed -n -e '/pattern1/,/pattern2/p' example.txt


Depending on how efficiency-critical you are (how big the files are), I'd probably be lazy and use a second pass to delete the pattern2 markers:



cat example.txt 
| sed -n -e '/pattern1/,/pattern2/p'
| sed -e '/pattern2/d'


(Yes, this is a useless use of cat, because I prefer the readability of chaining several piping filters over the performance loss, and I'm not sure right now if < example.txt | sed ... | sed ... is mandated by POSIX or just an extension that happens to be present in bash and zsh.)






share|improve this answer













Matching between lines between patterns, including either boundary, is a standard range selector in sed:



sed -n -e '/pattern1/,/pattern2/p' example.txt


Depending on how efficiency-critical you are (how big the files are), I'd probably be lazy and use a second pass to delete the pattern2 markers:



cat example.txt 
| sed -n -e '/pattern1/,/pattern2/p'
| sed -e '/pattern2/d'


(Yes, this is a useless use of cat, because I prefer the readability of chaining several piping filters over the performance loss, and I'm not sure right now if < example.txt | sed ... | sed ... is mandated by POSIX or just an extension that happens to be present in bash and zsh.)







share|improve this answer













share|improve this answer



share|improve this answer











answered Apr 20 at 10:32









Ulrich Schwarz

8,84512643




8,84512643







  • 1




    Also, sed -n '/pattern1/,/pattern2//pattern2/d;p;' file.
    – Kusalananda
    Apr 20 at 10:45













  • 1




    Also, sed -n '/pattern1/,/pattern2//pattern2/d;p;' file.
    – Kusalananda
    Apr 20 at 10:45








1




1




Also, sed -n '/pattern1/,/pattern2//pattern2/d;p;' file.
– Kusalananda
Apr 20 at 10:45





Also, sed -n '/pattern1/,/pattern2//pattern2/d;p;' file.
– Kusalananda
Apr 20 at 10:45













 

draft saved


draft discarded


























 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f438906%2fprint-output-matching-pattern-till-other-pattern-match%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

How to check contact read email or not when send email to Individual?

Bahrain

Postfix configuration issue with fips on centos 7; mailgun relay