Print text between two patterns not containing a particular word
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I want to print some text between two patterns which doesn't contain a particular word
input text is
HEADER asdf asd
asd COW assd
TAIL sdfsdfs
HEADER asdf asd
sdfsd DOG sdfsdfsdf
TAIL sdfsdfs
HEADER asdf asd
sdfsd MONKEY sdfsdfsdf
TAIL sdfsdfs
output needed is
HEADER asdf asd
asd COW assd
TAIL sdfsdfs
HEADER asdf asd
sdfsd MONKEY sdfsdfsdf
TAIL sdfsdfs
conceptually something like this is needed
awk '/HEADER/,!/DOG/,TAIL' text
text-processing sed awk text
add a comment |
I want to print some text between two patterns which doesn't contain a particular word
input text is
HEADER asdf asd
asd COW assd
TAIL sdfsdfs
HEADER asdf asd
sdfsd DOG sdfsdfsdf
TAIL sdfsdfs
HEADER asdf asd
sdfsd MONKEY sdfsdfsdf
TAIL sdfsdfs
output needed is
HEADER asdf asd
asd COW assd
TAIL sdfsdfs
HEADER asdf asd
sdfsd MONKEY sdfsdfsdf
TAIL sdfsdfs
conceptually something like this is needed
awk '/HEADER/,!/DOG/,TAIL' text
text-processing sed awk text
The empty line in your output is not between HEADER and TAIL in your input file so be more specific as to what should be preserved in the output...
– don_crissti
May 21 '15 at 13:37
Can youHEAD
orTAIL
lines containDOG
? If yes, is that ground for exclusion?
– Stéphane Chazelas
May 21 '15 at 15:39
HEAD OR TAIL will not contain DOG. I just have to skip the whole segments(between HEAD and TAIL,both inclusive) if there is a DOG between the HEAD and TAIL.
– user3905367
May 22 '15 at 3:22
add a comment |
I want to print some text between two patterns which doesn't contain a particular word
input text is
HEADER asdf asd
asd COW assd
TAIL sdfsdfs
HEADER asdf asd
sdfsd DOG sdfsdfsdf
TAIL sdfsdfs
HEADER asdf asd
sdfsd MONKEY sdfsdfsdf
TAIL sdfsdfs
output needed is
HEADER asdf asd
asd COW assd
TAIL sdfsdfs
HEADER asdf asd
sdfsd MONKEY sdfsdfsdf
TAIL sdfsdfs
conceptually something like this is needed
awk '/HEADER/,!/DOG/,TAIL' text
text-processing sed awk text
I want to print some text between two patterns which doesn't contain a particular word
input text is
HEADER asdf asd
asd COW assd
TAIL sdfsdfs
HEADER asdf asd
sdfsd DOG sdfsdfsdf
TAIL sdfsdfs
HEADER asdf asd
sdfsd MONKEY sdfsdfsdf
TAIL sdfsdfs
output needed is
HEADER asdf asd
asd COW assd
TAIL sdfsdfs
HEADER asdf asd
sdfsd MONKEY sdfsdfsdf
TAIL sdfsdfs
conceptually something like this is needed
awk '/HEADER/,!/DOG/,TAIL' text
text-processing sed awk text
text-processing sed awk text
edited Mar 9 at 13:23
Rui F Ribeiro
41.9k1483142
41.9k1483142
asked May 21 '15 at 13:18
user3905367user3905367
111
111
The empty line in your output is not between HEADER and TAIL in your input file so be more specific as to what should be preserved in the output...
– don_crissti
May 21 '15 at 13:37
Can youHEAD
orTAIL
lines containDOG
? If yes, is that ground for exclusion?
– Stéphane Chazelas
May 21 '15 at 15:39
HEAD OR TAIL will not contain DOG. I just have to skip the whole segments(between HEAD and TAIL,both inclusive) if there is a DOG between the HEAD and TAIL.
– user3905367
May 22 '15 at 3:22
add a comment |
The empty line in your output is not between HEADER and TAIL in your input file so be more specific as to what should be preserved in the output...
– don_crissti
May 21 '15 at 13:37
Can youHEAD
orTAIL
lines containDOG
? If yes, is that ground for exclusion?
– Stéphane Chazelas
May 21 '15 at 15:39
HEAD OR TAIL will not contain DOG. I just have to skip the whole segments(between HEAD and TAIL,both inclusive) if there is a DOG between the HEAD and TAIL.
– user3905367
May 22 '15 at 3:22
The empty line in your output is not between HEADER and TAIL in your input file so be more specific as to what should be preserved in the output...
– don_crissti
May 21 '15 at 13:37
The empty line in your output is not between HEADER and TAIL in your input file so be more specific as to what should be preserved in the output...
– don_crissti
May 21 '15 at 13:37
Can you
HEAD
or TAIL
lines contain DOG
? If yes, is that ground for exclusion?– Stéphane Chazelas
May 21 '15 at 15:39
Can you
HEAD
or TAIL
lines contain DOG
? If yes, is that ground for exclusion?– Stéphane Chazelas
May 21 '15 at 15:39
HEAD OR TAIL will not contain DOG. I just have to skip the whole segments(between HEAD and TAIL,both inclusive) if there is a DOG between the HEAD and TAIL.
– user3905367
May 22 '15 at 3:22
HEAD OR TAIL will not contain DOG. I just have to skip the whole segments(between HEAD and TAIL,both inclusive) if there is a DOG between the HEAD and TAIL.
– user3905367
May 22 '15 at 3:22
add a comment |
2 Answers
2
active
oldest
votes
With perl
:
perl -0777 -lne 'print for grep !/DOG/, /^HEADER.*?TAIL.*?n/mgs' your-file
With awk
:
awk '! inside if (/^HEADER/) inside = 1; skip = 0; content = "" else next
/DOG/skip = 1; next
! skip content=content $0 "n"
/^TAIL/ if (!skip) printf "%s", content; inside = 0' your-file
add a comment |
If there is no other limitation here your script
sed '/^HEADER/:1;N;/TAIL/!b1;/DOG/d' text
Just for fun other variants of awk
:
one:
awk '
BEGINprn=1
/^HEADER/block=1
block
if(/DOG/)
prn=0
if(!/^TAIL/)
line=line $0 "n"
next
prnprint line $0
/^TAIL/
block=0
prn=1
line=""
' text
two:
awk '
/^HEADER/
line=$0
while(!/TAIL/)
getline
line=line "n" $0
if(line !~ /DOG/)
print line
next
1' text
add a comment |
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',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
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
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f204822%2fprint-text-between-two-patterns-not-containing-a-particular-word%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
With perl
:
perl -0777 -lne 'print for grep !/DOG/, /^HEADER.*?TAIL.*?n/mgs' your-file
With awk
:
awk '! inside if (/^HEADER/) inside = 1; skip = 0; content = "" else next
/DOG/skip = 1; next
! skip content=content $0 "n"
/^TAIL/ if (!skip) printf "%s", content; inside = 0' your-file
add a comment |
With perl
:
perl -0777 -lne 'print for grep !/DOG/, /^HEADER.*?TAIL.*?n/mgs' your-file
With awk
:
awk '! inside if (/^HEADER/) inside = 1; skip = 0; content = "" else next
/DOG/skip = 1; next
! skip content=content $0 "n"
/^TAIL/ if (!skip) printf "%s", content; inside = 0' your-file
add a comment |
With perl
:
perl -0777 -lne 'print for grep !/DOG/, /^HEADER.*?TAIL.*?n/mgs' your-file
With awk
:
awk '! inside if (/^HEADER/) inside = 1; skip = 0; content = "" else next
/DOG/skip = 1; next
! skip content=content $0 "n"
/^TAIL/ if (!skip) printf "%s", content; inside = 0' your-file
With perl
:
perl -0777 -lne 'print for grep !/DOG/, /^HEADER.*?TAIL.*?n/mgs' your-file
With awk
:
awk '! inside if (/^HEADER/) inside = 1; skip = 0; content = "" else next
/DOG/skip = 1; next
! skip content=content $0 "n"
/^TAIL/ if (!skip) printf "%s", content; inside = 0' your-file
edited May 21 '15 at 14:32
answered May 21 '15 at 13:58
Stéphane ChazelasStéphane Chazelas
313k57592948
313k57592948
add a comment |
add a comment |
If there is no other limitation here your script
sed '/^HEADER/:1;N;/TAIL/!b1;/DOG/d' text
Just for fun other variants of awk
:
one:
awk '
BEGINprn=1
/^HEADER/block=1
block
if(/DOG/)
prn=0
if(!/^TAIL/)
line=line $0 "n"
next
prnprint line $0
/^TAIL/
block=0
prn=1
line=""
' text
two:
awk '
/^HEADER/
line=$0
while(!/TAIL/)
getline
line=line "n" $0
if(line !~ /DOG/)
print line
next
1' text
add a comment |
If there is no other limitation here your script
sed '/^HEADER/:1;N;/TAIL/!b1;/DOG/d' text
Just for fun other variants of awk
:
one:
awk '
BEGINprn=1
/^HEADER/block=1
block
if(/DOG/)
prn=0
if(!/^TAIL/)
line=line $0 "n"
next
prnprint line $0
/^TAIL/
block=0
prn=1
line=""
' text
two:
awk '
/^HEADER/
line=$0
while(!/TAIL/)
getline
line=line "n" $0
if(line !~ /DOG/)
print line
next
1' text
add a comment |
If there is no other limitation here your script
sed '/^HEADER/:1;N;/TAIL/!b1;/DOG/d' text
Just for fun other variants of awk
:
one:
awk '
BEGINprn=1
/^HEADER/block=1
block
if(/DOG/)
prn=0
if(!/^TAIL/)
line=line $0 "n"
next
prnprint line $0
/^TAIL/
block=0
prn=1
line=""
' text
two:
awk '
/^HEADER/
line=$0
while(!/TAIL/)
getline
line=line "n" $0
if(line !~ /DOG/)
print line
next
1' text
If there is no other limitation here your script
sed '/^HEADER/:1;N;/TAIL/!b1;/DOG/d' text
Just for fun other variants of awk
:
one:
awk '
BEGINprn=1
/^HEADER/block=1
block
if(/DOG/)
prn=0
if(!/^TAIL/)
line=line $0 "n"
next
prnprint line $0
/^TAIL/
block=0
prn=1
line=""
' text
two:
awk '
/^HEADER/
line=$0
while(!/TAIL/)
getline
line=line "n" $0
if(line !~ /DOG/)
print line
next
1' text
edited May 21 '15 at 15:41
answered May 21 '15 at 14:44
CostasCostas
12.7k1129
12.7k1129
add a comment |
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f204822%2fprint-text-between-two-patterns-not-containing-a-particular-word%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
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
Required, but never shown
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
Required, but never shown
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
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
The empty line in your output is not between HEADER and TAIL in your input file so be more specific as to what should be preserved in the output...
– don_crissti
May 21 '15 at 13:37
Can you
HEAD
orTAIL
lines containDOG
? If yes, is that ground for exclusion?– Stéphane Chazelas
May 21 '15 at 15:39
HEAD OR TAIL will not contain DOG. I just have to skip the whole segments(between HEAD and TAIL,both inclusive) if there is a DOG between the HEAD and TAIL.
– user3905367
May 22 '15 at 3:22