Print output matching pattern till other pattern match
Clash 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.
shell-script text-processing python
add a comment |Â
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.
shell-script text-processing python
Im absolutely sure this has a duplicate, but can't find it with a cursory search.
â Kusalananda
Apr 20 at 10:43
add a comment |Â
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.
shell-script text-processing python
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.
shell-script text-processing python
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
add a comment |Â
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
add a comment |Â
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.)
1
Also,sed -n '/pattern1/,/pattern2//pattern2/d;p;' file
.
â Kusalananda
Apr 20 at 10:45
add a comment |Â
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.)
1
Also,sed -n '/pattern1/,/pattern2//pattern2/d;p;' file
.
â Kusalananda
Apr 20 at 10:45
add a comment |Â
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.)
1
Also,sed -n '/pattern1/,/pattern2//pattern2/d;p;' file
.
â Kusalananda
Apr 20 at 10:45
add a comment |Â
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.)
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.)
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
add a comment |Â
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
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Im absolutely sure this has a duplicate, but can't find it with a cursory search.
â Kusalananda
Apr 20 at 10:43