Grep between patterns on separate lines or single line
Clash Royale CLAN TAG#URR8PPP
So what I would need is for grep to match only the text in between (and including) my match pattern.
So something like this (don't mind the text, it's just some garble :D):
asdgfasd gasd gdas g This will be this one day ksjadnbalsdkbgas asd gasdg
asdgasdgasdg dasg dasg dasg This will be this next day adf gdsf gdsf sdfh dsfhdfsh
asdf asdf asd fesf dsfasd f This will won' not this day asdgadsgaseg as dvf as d vfa se v asd
dasfasdfdas fase fasdfasefase fasdf This not what shoes day asdjbna;sdgbva;sdkbcvd;lasb ;lkbasi hasdli glais g
So what I want is something like this:cat theabovetext|grep -E "^This * day$"
To output this:
This will be this one day
This will be this next day
This will won' not this day
This not what shoes day
So basically I want to ONLY get the text in between This and Day (including This and day) regardless of how many characters there are in between, and regardless of how many characters there are before This and after Day.
Also this needs to work even if the input is all on a single line, so this:
asdgfasd gasd gdas g This will be this one day ksjadnbalsdkbgas asd gasdg asdgasdgasdg dasg dasg dasg This will be this next day adf gdsf gdsf sdfh dsfhdfsh asdf asdf asd fesf dsfasd f This will won' not this day asdgadsgaseg as dvf as d vfa se v asd dasfasdfdas fase fasdfasefase fasdf This not what shoes day asdjbna;sdgbva;sdkbcvd;lasb ;lkbasi hasdli glais g
Has to output this:
This will be this one day This will be this next day This will won' not this day This not what shoes day
N.B. the output here is still on a single line.
grep wildcards
add a comment |
So what I would need is for grep to match only the text in between (and including) my match pattern.
So something like this (don't mind the text, it's just some garble :D):
asdgfasd gasd gdas g This will be this one day ksjadnbalsdkbgas asd gasdg
asdgasdgasdg dasg dasg dasg This will be this next day adf gdsf gdsf sdfh dsfhdfsh
asdf asdf asd fesf dsfasd f This will won' not this day asdgadsgaseg as dvf as d vfa se v asd
dasfasdfdas fase fasdfasefase fasdf This not what shoes day asdjbna;sdgbva;sdkbcvd;lasb ;lkbasi hasdli glais g
So what I want is something like this:cat theabovetext|grep -E "^This * day$"
To output this:
This will be this one day
This will be this next day
This will won' not this day
This not what shoes day
So basically I want to ONLY get the text in between This and Day (including This and day) regardless of how many characters there are in between, and regardless of how many characters there are before This and after Day.
Also this needs to work even if the input is all on a single line, so this:
asdgfasd gasd gdas g This will be this one day ksjadnbalsdkbgas asd gasdg asdgasdgasdg dasg dasg dasg This will be this next day adf gdsf gdsf sdfh dsfhdfsh asdf asdf asd fesf dsfasd f This will won' not this day asdgadsgaseg as dvf as d vfa se v asd dasfasdfdas fase fasdfasefase fasdf This not what shoes day asdjbna;sdgbva;sdkbcvd;lasb ;lkbasi hasdli glais g
Has to output this:
This will be this one day This will be this next day This will won' not this day This not what shoes day
N.B. the output here is still on a single line.
grep wildcards
1
What version ofgrep
are you looking to use? GNU with perl style regex will can do it, otherwise are you open to non-grep
solutions?
– Eric Renouf
Jan 1 at 22:33
grep (GNU grep) 2.20, but if it's awk or sed, I can work with those as well
– superme
Jan 1 at 22:34
@superme I've attempted to emphasise important parts of your question. Feel free to re-edit if I have misread the question.
– Sparhawk
Jan 1 at 23:05
add a comment |
So what I would need is for grep to match only the text in between (and including) my match pattern.
So something like this (don't mind the text, it's just some garble :D):
asdgfasd gasd gdas g This will be this one day ksjadnbalsdkbgas asd gasdg
asdgasdgasdg dasg dasg dasg This will be this next day adf gdsf gdsf sdfh dsfhdfsh
asdf asdf asd fesf dsfasd f This will won' not this day asdgadsgaseg as dvf as d vfa se v asd
dasfasdfdas fase fasdfasefase fasdf This not what shoes day asdjbna;sdgbva;sdkbcvd;lasb ;lkbasi hasdli glais g
So what I want is something like this:cat theabovetext|grep -E "^This * day$"
To output this:
This will be this one day
This will be this next day
This will won' not this day
This not what shoes day
So basically I want to ONLY get the text in between This and Day (including This and day) regardless of how many characters there are in between, and regardless of how many characters there are before This and after Day.
Also this needs to work even if the input is all on a single line, so this:
asdgfasd gasd gdas g This will be this one day ksjadnbalsdkbgas asd gasdg asdgasdgasdg dasg dasg dasg This will be this next day adf gdsf gdsf sdfh dsfhdfsh asdf asdf asd fesf dsfasd f This will won' not this day asdgadsgaseg as dvf as d vfa se v asd dasfasdfdas fase fasdfasefase fasdf This not what shoes day asdjbna;sdgbva;sdkbcvd;lasb ;lkbasi hasdli glais g
Has to output this:
This will be this one day This will be this next day This will won' not this day This not what shoes day
N.B. the output here is still on a single line.
grep wildcards
So what I would need is for grep to match only the text in between (and including) my match pattern.
So something like this (don't mind the text, it's just some garble :D):
asdgfasd gasd gdas g This will be this one day ksjadnbalsdkbgas asd gasdg
asdgasdgasdg dasg dasg dasg This will be this next day adf gdsf gdsf sdfh dsfhdfsh
asdf asdf asd fesf dsfasd f This will won' not this day asdgadsgaseg as dvf as d vfa se v asd
dasfasdfdas fase fasdfasefase fasdf This not what shoes day asdjbna;sdgbva;sdkbcvd;lasb ;lkbasi hasdli glais g
So what I want is something like this:cat theabovetext|grep -E "^This * day$"
To output this:
This will be this one day
This will be this next day
This will won' not this day
This not what shoes day
So basically I want to ONLY get the text in between This and Day (including This and day) regardless of how many characters there are in between, and regardless of how many characters there are before This and after Day.
Also this needs to work even if the input is all on a single line, so this:
asdgfasd gasd gdas g This will be this one day ksjadnbalsdkbgas asd gasdg asdgasdgasdg dasg dasg dasg This will be this next day adf gdsf gdsf sdfh dsfhdfsh asdf asdf asd fesf dsfasd f This will won' not this day asdgadsgaseg as dvf as d vfa se v asd dasfasdfdas fase fasdfasefase fasdf This not what shoes day asdjbna;sdgbva;sdkbcvd;lasb ;lkbasi hasdli glais g
Has to output this:
This will be this one day This will be this next day This will won' not this day This not what shoes day
N.B. the output here is still on a single line.
grep wildcards
grep wildcards
edited Jan 1 at 23:05
Sparhawk
9,38763992
9,38763992
asked Jan 1 at 22:29
supermesuperme
182
182
1
What version ofgrep
are you looking to use? GNU with perl style regex will can do it, otherwise are you open to non-grep
solutions?
– Eric Renouf
Jan 1 at 22:33
grep (GNU grep) 2.20, but if it's awk or sed, I can work with those as well
– superme
Jan 1 at 22:34
@superme I've attempted to emphasise important parts of your question. Feel free to re-edit if I have misread the question.
– Sparhawk
Jan 1 at 23:05
add a comment |
1
What version ofgrep
are you looking to use? GNU with perl style regex will can do it, otherwise are you open to non-grep
solutions?
– Eric Renouf
Jan 1 at 22:33
grep (GNU grep) 2.20, but if it's awk or sed, I can work with those as well
– superme
Jan 1 at 22:34
@superme I've attempted to emphasise important parts of your question. Feel free to re-edit if I have misread the question.
– Sparhawk
Jan 1 at 23:05
1
1
What version of
grep
are you looking to use? GNU with perl style regex will can do it, otherwise are you open to non-grep
solutions?– Eric Renouf
Jan 1 at 22:33
What version of
grep
are you looking to use? GNU with perl style regex will can do it, otherwise are you open to non-grep
solutions?– Eric Renouf
Jan 1 at 22:33
grep (GNU grep) 2.20, but if it's awk or sed, I can work with those as well
– superme
Jan 1 at 22:34
grep (GNU grep) 2.20, but if it's awk or sed, I can work with those as well
– superme
Jan 1 at 22:34
@superme I've attempted to emphasise important parts of your question. Feel free to re-edit if I have misread the question.
– Sparhawk
Jan 1 at 23:05
@superme I've attempted to emphasise important parts of your question. Feel free to re-edit if I have misread the question.
– Sparhawk
Jan 1 at 23:05
add a comment |
3 Answers
3
active
oldest
votes
If you want lines to be processed separately (your first example) but for multiple matches per line to be output on a single line (as in your second example), then I don't think that's possible with grep
alone.
However using the same This.*?day
non-greedy match in perl itself you can do
$ perl -lne 'print join " ", /This.*?day/g' theabovetext1
This will be this one day
This will be this next day
This will won' not this day
This not what shoes day
while for the single-line input
$ perl -lne 'print join " ", /This.*?day/g' theabovetext2
This will be this one day This will be this next day This will won' not this day This not what shoes day
add a comment |
With GNU grep
you could do the following:
grep -o 'This.*day' theabovetext
(note that you don't need cat
since grep
knows how to read files)
The -o
flag says to show only the parts of the line that match the pattern.
I suspect other versions of grep
support this flag as well, but it's not in POSIX, so it's not portable necessarily.
1
[me@there ~]$ grep -o 'This.*day' theabovetext This will be this one day ksjadnbalsdkbgas asd gasdg asdgasdgasdg dasg dasg dasg This will be this next day adf gdsf gdsf sdfh dsfhdfsh asdf asdf asd fesf dsfasd f This will won' not this day asdgadsgaseg as dvf as d vfa se v asd dasfasdfdas fase fasdfasefase fasdf This not what shoes day
it only removes the first part and the last part of the gibberish it doesn't remove it in between matches
– superme
Jan 1 at 22:43
3
You can use PCRE mode so that the quantifier can be made non-greedygrep -oP 'This.*?day' theabovetext
however this will place each match on a separate line in both cases
– steeldriver
Jan 1 at 22:51
@steeldriver that worked, thanks! I can't select your comment as the right answer though. One thing I'd like to know though: how come with'This.*day'
it doesn't match it, but adding the?
matches it?
– superme
Jan 1 at 23:19
@superme I didn't post it as an answer because I don't believe it satisfies your requirement that "the output here is still on a single line" for the case where the input is on a single line
– steeldriver
Jan 1 at 23:22
1
@superme it modifies the*
to make it non-greedy - see for example Perl regex tutorial: non-greedy expressions
– steeldriver
Jan 1 at 23:41
|
show 2 more comments
Eric Renouf's response does most of the work. And steeldriver's comment cuts out the extra text within a given line by making it non-greedy.
So it seems like: grep -oP 'This.*?day' theabovetext
does everything you want except the output is on multiple lines.
To put the output on one line you could do grep -oP 'This.*?day' theabovetext | tr 'n' ' '
. This addition simply replaces newline breaks with spaces*
*This will replace all output newlines with spaces. So if your initial input is line separated, these newline breaks will be lost.
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%2f491925%2fgrep-between-patterns-on-separate-lines-or-single-line%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
If you want lines to be processed separately (your first example) but for multiple matches per line to be output on a single line (as in your second example), then I don't think that's possible with grep
alone.
However using the same This.*?day
non-greedy match in perl itself you can do
$ perl -lne 'print join " ", /This.*?day/g' theabovetext1
This will be this one day
This will be this next day
This will won' not this day
This not what shoes day
while for the single-line input
$ perl -lne 'print join " ", /This.*?day/g' theabovetext2
This will be this one day This will be this next day This will won' not this day This not what shoes day
add a comment |
If you want lines to be processed separately (your first example) but for multiple matches per line to be output on a single line (as in your second example), then I don't think that's possible with grep
alone.
However using the same This.*?day
non-greedy match in perl itself you can do
$ perl -lne 'print join " ", /This.*?day/g' theabovetext1
This will be this one day
This will be this next day
This will won' not this day
This not what shoes day
while for the single-line input
$ perl -lne 'print join " ", /This.*?day/g' theabovetext2
This will be this one day This will be this next day This will won' not this day This not what shoes day
add a comment |
If you want lines to be processed separately (your first example) but for multiple matches per line to be output on a single line (as in your second example), then I don't think that's possible with grep
alone.
However using the same This.*?day
non-greedy match in perl itself you can do
$ perl -lne 'print join " ", /This.*?day/g' theabovetext1
This will be this one day
This will be this next day
This will won' not this day
This not what shoes day
while for the single-line input
$ perl -lne 'print join " ", /This.*?day/g' theabovetext2
This will be this one day This will be this next day This will won' not this day This not what shoes day
If you want lines to be processed separately (your first example) but for multiple matches per line to be output on a single line (as in your second example), then I don't think that's possible with grep
alone.
However using the same This.*?day
non-greedy match in perl itself you can do
$ perl -lne 'print join " ", /This.*?day/g' theabovetext1
This will be this one day
This will be this next day
This will won' not this day
This not what shoes day
while for the single-line input
$ perl -lne 'print join " ", /This.*?day/g' theabovetext2
This will be this one day This will be this next day This will won' not this day This not what shoes day
answered Jan 2 at 0:18
steeldriversteeldriver
34.9k35184
34.9k35184
add a comment |
add a comment |
With GNU grep
you could do the following:
grep -o 'This.*day' theabovetext
(note that you don't need cat
since grep
knows how to read files)
The -o
flag says to show only the parts of the line that match the pattern.
I suspect other versions of grep
support this flag as well, but it's not in POSIX, so it's not portable necessarily.
1
[me@there ~]$ grep -o 'This.*day' theabovetext This will be this one day ksjadnbalsdkbgas asd gasdg asdgasdgasdg dasg dasg dasg This will be this next day adf gdsf gdsf sdfh dsfhdfsh asdf asdf asd fesf dsfasd f This will won' not this day asdgadsgaseg as dvf as d vfa se v asd dasfasdfdas fase fasdfasefase fasdf This not what shoes day
it only removes the first part and the last part of the gibberish it doesn't remove it in between matches
– superme
Jan 1 at 22:43
3
You can use PCRE mode so that the quantifier can be made non-greedygrep -oP 'This.*?day' theabovetext
however this will place each match on a separate line in both cases
– steeldriver
Jan 1 at 22:51
@steeldriver that worked, thanks! I can't select your comment as the right answer though. One thing I'd like to know though: how come with'This.*day'
it doesn't match it, but adding the?
matches it?
– superme
Jan 1 at 23:19
@superme I didn't post it as an answer because I don't believe it satisfies your requirement that "the output here is still on a single line" for the case where the input is on a single line
– steeldriver
Jan 1 at 23:22
1
@superme it modifies the*
to make it non-greedy - see for example Perl regex tutorial: non-greedy expressions
– steeldriver
Jan 1 at 23:41
|
show 2 more comments
With GNU grep
you could do the following:
grep -o 'This.*day' theabovetext
(note that you don't need cat
since grep
knows how to read files)
The -o
flag says to show only the parts of the line that match the pattern.
I suspect other versions of grep
support this flag as well, but it's not in POSIX, so it's not portable necessarily.
1
[me@there ~]$ grep -o 'This.*day' theabovetext This will be this one day ksjadnbalsdkbgas asd gasdg asdgasdgasdg dasg dasg dasg This will be this next day adf gdsf gdsf sdfh dsfhdfsh asdf asdf asd fesf dsfasd f This will won' not this day asdgadsgaseg as dvf as d vfa se v asd dasfasdfdas fase fasdfasefase fasdf This not what shoes day
it only removes the first part and the last part of the gibberish it doesn't remove it in between matches
– superme
Jan 1 at 22:43
3
You can use PCRE mode so that the quantifier can be made non-greedygrep -oP 'This.*?day' theabovetext
however this will place each match on a separate line in both cases
– steeldriver
Jan 1 at 22:51
@steeldriver that worked, thanks! I can't select your comment as the right answer though. One thing I'd like to know though: how come with'This.*day'
it doesn't match it, but adding the?
matches it?
– superme
Jan 1 at 23:19
@superme I didn't post it as an answer because I don't believe it satisfies your requirement that "the output here is still on a single line" for the case where the input is on a single line
– steeldriver
Jan 1 at 23:22
1
@superme it modifies the*
to make it non-greedy - see for example Perl regex tutorial: non-greedy expressions
– steeldriver
Jan 1 at 23:41
|
show 2 more comments
With GNU grep
you could do the following:
grep -o 'This.*day' theabovetext
(note that you don't need cat
since grep
knows how to read files)
The -o
flag says to show only the parts of the line that match the pattern.
I suspect other versions of grep
support this flag as well, but it's not in POSIX, so it's not portable necessarily.
With GNU grep
you could do the following:
grep -o 'This.*day' theabovetext
(note that you don't need cat
since grep
knows how to read files)
The -o
flag says to show only the parts of the line that match the pattern.
I suspect other versions of grep
support this flag as well, but it's not in POSIX, so it's not portable necessarily.
answered Jan 1 at 22:38
Eric RenoufEric Renouf
13.4k43050
13.4k43050
1
[me@there ~]$ grep -o 'This.*day' theabovetext This will be this one day ksjadnbalsdkbgas asd gasdg asdgasdgasdg dasg dasg dasg This will be this next day adf gdsf gdsf sdfh dsfhdfsh asdf asdf asd fesf dsfasd f This will won' not this day asdgadsgaseg as dvf as d vfa se v asd dasfasdfdas fase fasdfasefase fasdf This not what shoes day
it only removes the first part and the last part of the gibberish it doesn't remove it in between matches
– superme
Jan 1 at 22:43
3
You can use PCRE mode so that the quantifier can be made non-greedygrep -oP 'This.*?day' theabovetext
however this will place each match on a separate line in both cases
– steeldriver
Jan 1 at 22:51
@steeldriver that worked, thanks! I can't select your comment as the right answer though. One thing I'd like to know though: how come with'This.*day'
it doesn't match it, but adding the?
matches it?
– superme
Jan 1 at 23:19
@superme I didn't post it as an answer because I don't believe it satisfies your requirement that "the output here is still on a single line" for the case where the input is on a single line
– steeldriver
Jan 1 at 23:22
1
@superme it modifies the*
to make it non-greedy - see for example Perl regex tutorial: non-greedy expressions
– steeldriver
Jan 1 at 23:41
|
show 2 more comments
1
[me@there ~]$ grep -o 'This.*day' theabovetext This will be this one day ksjadnbalsdkbgas asd gasdg asdgasdgasdg dasg dasg dasg This will be this next day adf gdsf gdsf sdfh dsfhdfsh asdf asdf asd fesf dsfasd f This will won' not this day asdgadsgaseg as dvf as d vfa se v asd dasfasdfdas fase fasdfasefase fasdf This not what shoes day
it only removes the first part and the last part of the gibberish it doesn't remove it in between matches
– superme
Jan 1 at 22:43
3
You can use PCRE mode so that the quantifier can be made non-greedygrep -oP 'This.*?day' theabovetext
however this will place each match on a separate line in both cases
– steeldriver
Jan 1 at 22:51
@steeldriver that worked, thanks! I can't select your comment as the right answer though. One thing I'd like to know though: how come with'This.*day'
it doesn't match it, but adding the?
matches it?
– superme
Jan 1 at 23:19
@superme I didn't post it as an answer because I don't believe it satisfies your requirement that "the output here is still on a single line" for the case where the input is on a single line
– steeldriver
Jan 1 at 23:22
1
@superme it modifies the*
to make it non-greedy - see for example Perl regex tutorial: non-greedy expressions
– steeldriver
Jan 1 at 23:41
1
1
[me@there ~]$ grep -o 'This.*day' theabovetext This will be this one day ksjadnbalsdkbgas asd gasdg asdgasdgasdg dasg dasg dasg This will be this next day adf gdsf gdsf sdfh dsfhdfsh asdf asdf asd fesf dsfasd f This will won' not this day asdgadsgaseg as dvf as d vfa se v asd dasfasdfdas fase fasdfasefase fasdf This not what shoes day
it only removes the first part and the last part of the gibberish it doesn't remove it in between matches– superme
Jan 1 at 22:43
[me@there ~]$ grep -o 'This.*day' theabovetext This will be this one day ksjadnbalsdkbgas asd gasdg asdgasdgasdg dasg dasg dasg This will be this next day adf gdsf gdsf sdfh dsfhdfsh asdf asdf asd fesf dsfasd f This will won' not this day asdgadsgaseg as dvf as d vfa se v asd dasfasdfdas fase fasdfasefase fasdf This not what shoes day
it only removes the first part and the last part of the gibberish it doesn't remove it in between matches– superme
Jan 1 at 22:43
3
3
You can use PCRE mode so that the quantifier can be made non-greedy
grep -oP 'This.*?day' theabovetext
however this will place each match on a separate line in both cases– steeldriver
Jan 1 at 22:51
You can use PCRE mode so that the quantifier can be made non-greedy
grep -oP 'This.*?day' theabovetext
however this will place each match on a separate line in both cases– steeldriver
Jan 1 at 22:51
@steeldriver that worked, thanks! I can't select your comment as the right answer though. One thing I'd like to know though: how come with
'This.*day'
it doesn't match it, but adding the ?
matches it?– superme
Jan 1 at 23:19
@steeldriver that worked, thanks! I can't select your comment as the right answer though. One thing I'd like to know though: how come with
'This.*day'
it doesn't match it, but adding the ?
matches it?– superme
Jan 1 at 23:19
@superme I didn't post it as an answer because I don't believe it satisfies your requirement that "the output here is still on a single line" for the case where the input is on a single line
– steeldriver
Jan 1 at 23:22
@superme I didn't post it as an answer because I don't believe it satisfies your requirement that "the output here is still on a single line" for the case where the input is on a single line
– steeldriver
Jan 1 at 23:22
1
1
@superme it modifies the
*
to make it non-greedy - see for example Perl regex tutorial: non-greedy expressions– steeldriver
Jan 1 at 23:41
@superme it modifies the
*
to make it non-greedy - see for example Perl regex tutorial: non-greedy expressions– steeldriver
Jan 1 at 23:41
|
show 2 more comments
Eric Renouf's response does most of the work. And steeldriver's comment cuts out the extra text within a given line by making it non-greedy.
So it seems like: grep -oP 'This.*?day' theabovetext
does everything you want except the output is on multiple lines.
To put the output on one line you could do grep -oP 'This.*?day' theabovetext | tr 'n' ' '
. This addition simply replaces newline breaks with spaces*
*This will replace all output newlines with spaces. So if your initial input is line separated, these newline breaks will be lost.
add a comment |
Eric Renouf's response does most of the work. And steeldriver's comment cuts out the extra text within a given line by making it non-greedy.
So it seems like: grep -oP 'This.*?day' theabovetext
does everything you want except the output is on multiple lines.
To put the output on one line you could do grep -oP 'This.*?day' theabovetext | tr 'n' ' '
. This addition simply replaces newline breaks with spaces*
*This will replace all output newlines with spaces. So if your initial input is line separated, these newline breaks will be lost.
add a comment |
Eric Renouf's response does most of the work. And steeldriver's comment cuts out the extra text within a given line by making it non-greedy.
So it seems like: grep -oP 'This.*?day' theabovetext
does everything you want except the output is on multiple lines.
To put the output on one line you could do grep -oP 'This.*?day' theabovetext | tr 'n' ' '
. This addition simply replaces newline breaks with spaces*
*This will replace all output newlines with spaces. So if your initial input is line separated, these newline breaks will be lost.
Eric Renouf's response does most of the work. And steeldriver's comment cuts out the extra text within a given line by making it non-greedy.
So it seems like: grep -oP 'This.*?day' theabovetext
does everything you want except the output is on multiple lines.
To put the output on one line you could do grep -oP 'This.*?day' theabovetext | tr 'n' ' '
. This addition simply replaces newline breaks with spaces*
*This will replace all output newlines with spaces. So if your initial input is line separated, these newline breaks will be lost.
answered Jan 1 at 23:47
CrypteyaCrypteya
645
645
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%2f491925%2fgrep-between-patterns-on-separate-lines-or-single-line%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
1
What version of
grep
are you looking to use? GNU with perl style regex will can do it, otherwise are you open to non-grep
solutions?– Eric Renouf
Jan 1 at 22:33
grep (GNU grep) 2.20, but if it's awk or sed, I can work with those as well
– superme
Jan 1 at 22:34
@superme I've attempted to emphasise important parts of your question. Feel free to re-edit if I have misread the question.
– Sparhawk
Jan 1 at 23:05