grep: Always show context of N lines
Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
Given a file like
asdasd
123
X
456
X
789
asd
asd
asd
asd
If I run grep: grep -C3 'X'
on the above, I get the following results
asdasd
123
X
456
--
456
X
789
asd
asd
It appears that grep only shows the context of each match up to the previous or following match. Is there a way to get it to display the entire context regardless of whether the pattern is contained in it? Looking at man grep
, there seems to be no such option. The expected result is as follows:
asdasd
123
X
456
X
789
--
123
X
456
X
789
asd
asd
grep osx
New contributor
add a comment |
up vote
1
down vote
favorite
Given a file like
asdasd
123
X
456
X
789
asd
asd
asd
asd
If I run grep: grep -C3 'X'
on the above, I get the following results
asdasd
123
X
456
--
456
X
789
asd
asd
It appears that grep only shows the context of each match up to the previous or following match. Is there a way to get it to display the entire context regardless of whether the pattern is contained in it? Looking at man grep
, there seems to be no such option. The expected result is as follows:
asdasd
123
X
456
X
789
--
123
X
456
X
789
asd
asd
grep osx
New contributor
1
What version of OSX are you running, and what doesgrep --version
report? The (admittedly very old) Mac I just tried came with GNU grep 2.5.1, which produces something else again (the first 8 lines of the file, with no--
).
– JigglyNaga
Nov 20 at 10:10
if you okay with awk, you could adapt the second solution present in unix.stackexchange.com/questions/66196/… for your needs
– Sundeep
Nov 20 at 15:03
grep --version
reports "grep (BSD grep) 2.5.1-FreeBSD". Running on MacOS Mojave Version 10.14
– kanghj91
Nov 21 at 1:56
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
Given a file like
asdasd
123
X
456
X
789
asd
asd
asd
asd
If I run grep: grep -C3 'X'
on the above, I get the following results
asdasd
123
X
456
--
456
X
789
asd
asd
It appears that grep only shows the context of each match up to the previous or following match. Is there a way to get it to display the entire context regardless of whether the pattern is contained in it? Looking at man grep
, there seems to be no such option. The expected result is as follows:
asdasd
123
X
456
X
789
--
123
X
456
X
789
asd
asd
grep osx
New contributor
Given a file like
asdasd
123
X
456
X
789
asd
asd
asd
asd
If I run grep: grep -C3 'X'
on the above, I get the following results
asdasd
123
X
456
--
456
X
789
asd
asd
It appears that grep only shows the context of each match up to the previous or following match. Is there a way to get it to display the entire context regardless of whether the pattern is contained in it? Looking at man grep
, there seems to be no such option. The expected result is as follows:
asdasd
123
X
456
X
789
--
123
X
456
X
789
asd
asd
grep osx
grep osx
New contributor
New contributor
edited Nov 20 at 9:25
New contributor
asked Nov 20 at 8:57
kanghj91
63
63
New contributor
New contributor
1
What version of OSX are you running, and what doesgrep --version
report? The (admittedly very old) Mac I just tried came with GNU grep 2.5.1, which produces something else again (the first 8 lines of the file, with no--
).
– JigglyNaga
Nov 20 at 10:10
if you okay with awk, you could adapt the second solution present in unix.stackexchange.com/questions/66196/… for your needs
– Sundeep
Nov 20 at 15:03
grep --version
reports "grep (BSD grep) 2.5.1-FreeBSD". Running on MacOS Mojave Version 10.14
– kanghj91
Nov 21 at 1:56
add a comment |
1
What version of OSX are you running, and what doesgrep --version
report? The (admittedly very old) Mac I just tried came with GNU grep 2.5.1, which produces something else again (the first 8 lines of the file, with no--
).
– JigglyNaga
Nov 20 at 10:10
if you okay with awk, you could adapt the second solution present in unix.stackexchange.com/questions/66196/… for your needs
– Sundeep
Nov 20 at 15:03
grep --version
reports "grep (BSD grep) 2.5.1-FreeBSD". Running on MacOS Mojave Version 10.14
– kanghj91
Nov 21 at 1:56
1
1
What version of OSX are you running, and what does
grep --version
report? The (admittedly very old) Mac I just tried came with GNU grep 2.5.1, which produces something else again (the first 8 lines of the file, with no --
).– JigglyNaga
Nov 20 at 10:10
What version of OSX are you running, and what does
grep --version
report? The (admittedly very old) Mac I just tried came with GNU grep 2.5.1, which produces something else again (the first 8 lines of the file, with no --
).– JigglyNaga
Nov 20 at 10:10
if you okay with awk, you could adapt the second solution present in unix.stackexchange.com/questions/66196/… for your needs
– Sundeep
Nov 20 at 15:03
if you okay with awk, you could adapt the second solution present in unix.stackexchange.com/questions/66196/… for your needs
– Sundeep
Nov 20 at 15:03
grep --version
reports "grep (BSD grep) 2.5.1-FreeBSD". Running on MacOS Mojave Version 10.14– kanghj91
Nov 21 at 1:56
grep --version
reports "grep (BSD grep) 2.5.1-FreeBSD". Running on MacOS Mojave Version 10.14– kanghj91
Nov 21 at 1:56
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
Here's a short perl script to do it. It does read the whole file into memory, so might not be appropriate for really big files.
perl -0777 -snE '
@lines = split /n/;
for $idx (grep $lines[$_] =~ $p 0...$#lines)
say join "n", @lines[$idx-($n-1) .. $idx+($n-1)], "--";
' -- -n=3 -p='X' file
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
Here's a short perl script to do it. It does read the whole file into memory, so might not be appropriate for really big files.
perl -0777 -snE '
@lines = split /n/;
for $idx (grep $lines[$_] =~ $p 0...$#lines)
say join "n", @lines[$idx-($n-1) .. $idx+($n-1)], "--";
' -- -n=3 -p='X' file
add a comment |
up vote
0
down vote
Here's a short perl script to do it. It does read the whole file into memory, so might not be appropriate for really big files.
perl -0777 -snE '
@lines = split /n/;
for $idx (grep $lines[$_] =~ $p 0...$#lines)
say join "n", @lines[$idx-($n-1) .. $idx+($n-1)], "--";
' -- -n=3 -p='X' file
add a comment |
up vote
0
down vote
up vote
0
down vote
Here's a short perl script to do it. It does read the whole file into memory, so might not be appropriate for really big files.
perl -0777 -snE '
@lines = split /n/;
for $idx (grep $lines[$_] =~ $p 0...$#lines)
say join "n", @lines[$idx-($n-1) .. $idx+($n-1)], "--";
' -- -n=3 -p='X' file
Here's a short perl script to do it. It does read the whole file into memory, so might not be appropriate for really big files.
perl -0777 -snE '
@lines = split /n/;
for $idx (grep $lines[$_] =~ $p 0...$#lines)
say join "n", @lines[$idx-($n-1) .. $idx+($n-1)], "--";
' -- -n=3 -p='X' file
answered Nov 20 at 15:00
glenn jackman
49.4k469106
49.4k469106
add a comment |
add a comment |
kanghj91 is a new contributor. Be nice, and check out our Code of Conduct.
kanghj91 is a new contributor. Be nice, and check out our Code of Conduct.
kanghj91 is a new contributor. Be nice, and check out our Code of Conduct.
kanghj91 is a new contributor. Be nice, and check out our Code of Conduct.
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%2f482913%2fgrep-always-show-context-of-n-lines%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 OSX are you running, and what does
grep --version
report? The (admittedly very old) Mac I just tried came with GNU grep 2.5.1, which produces something else again (the first 8 lines of the file, with no--
).– JigglyNaga
Nov 20 at 10:10
if you okay with awk, you could adapt the second solution present in unix.stackexchange.com/questions/66196/… for your needs
– Sundeep
Nov 20 at 15:03
grep --version
reports "grep (BSD grep) 2.5.1-FreeBSD". Running on MacOS Mojave Version 10.14– kanghj91
Nov 21 at 1:56