grep STOP regex replacing -A option Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 00:00UTC (8:00pm US/Eastern) 2019 Community Moderator Election Results Why I closed the “Why is Kali so hard” questionTrouble with grep -o regexGrep 'OR' regex problemPrint a line in stdout that matches an expression if the output contains another expressionInterpreting regex wildcards with grepgrep -E regex syntax changed?Grep regex how toOverwrite file using AWK in a for loopFor a large directory, create a variable of the filenames which include lines which include the text string stored in another variableReturn multiple regex match groups from an nmap returned resultsHow to edit the entire file after match a grep pattern?
Trademark violation for app?
AppleTVs create a chatty alternate WiFi network
How does the math work when buying airline miles?
Amount of permutations on an NxNxN Rubik's Cube
Why weren't discrete x86 CPUs ever used in game hardware?
Why aren't air breathing engines used as small first stages?
How to install press fit bottom bracket into new frame
Is there hard evidence that the grant peer review system performs significantly better than random?
How does light 'choose' between wave and particle behaviour?
Effects on objects due to a brief relocation of massive amounts of mass
Time to Settle Down!
Chinese Seal on silk painting - what does it mean?
Morning, Afternoon, Night Kanji
Generate an RGB colour grid
Why wasn't DOSKEY integrated with COMMAND.COM?
Why is my ESD wriststrap failing with nitrile gloves on?
Crossing US/Canada Border for less than 24 hours
Significance of Cersei's obsession with elephants?
Selecting user stories during sprint planning
What do you call the main part of a joke?
Disembodied hand growing fangs
How often does castling occur in grandmaster games?
Why is Nikon 1.4g better when Nikon 1.8g is sharper?
Using audio cues to encourage good posture
grep STOP regex replacing -A option
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 00:00UTC (8:00pm US/Eastern)
2019 Community Moderator Election Results
Why I closed the “Why is Kali so hard” questionTrouble with grep -o regexGrep 'OR' regex problemPrint a line in stdout that matches an expression if the output contains another expressionInterpreting regex wildcards with grepgrep -E regex syntax changed?Grep regex how toOverwrite file using AWK in a for loopFor a large directory, create a variable of the filenames which include lines which include the text string stored in another variableReturn multiple regex match groups from an nmap returned resultsHow to edit the entire file after match a grep pattern?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I'm looking/searching for a way to make grep stop when finding the "|_" specific string after having found
the search string.
for example, i want to print ONLY the lines starting 2 lines above "address-info:" and stopping at line "|_"
AND NOT print "irrelevant lines in between" which BTW could by anything
INPUT:
Nmap scan report for ::1.2.3.4
Host script results:
| address-info:
| IPv4-compatible:
|_ IPv4 address: 1.2.3.4
...
irrelevant lines in between
irrelevant lines in between
irrelevant lines in between
...
Nmap scan report for ::ffff:1.2.3.4
Host script results:
| address-info:
| IPv4-mapped:
|_ IPv4 address: 1.2.3.4
...
irrelevant lines in between
irrelevant lines in between
irrelevant lines in between
...
Nmap scan report for 2001:0:506:708:282a:3d75:fefd:fcfb
Host script results:
| address-info:
| Teredo:
| Server IPv4 address: 5.6.7.8
| Client IPv4 address: 1.2.3.4
|_ UDP port: 49802
OUTPUT:
Nmap scan report for ::1.2.3.4
Host script results:
| address-info:
| IPv4-compatible:
|_ IPv4 address: 1.2.3.4
Nmap scan report for ::ffff:1.2.3.4
Host script results:
| address-info:
| IPv4-mapped:
|_ IPv4 address: 1.2.3.4
Nmap scan report for 2001:0:506:708:282a:3d75:fefd:fcfb
Host script results:
| address-info:
| Teredo:
| Server IPv4 address: 5.6.7.8
| Client IPv4 address: 1.2.3.4
|_ UDP port: 49802
I'v read man grep and found -A -B -C options, it's OK for -B as I know in advance how many line before but for -A
I gave an arbituray high value ie: 99999 in
grep -A99999 -B2 address-info: INPUT.txt
and piping in awk to find "|_"
awk 'BEGIN PAT=1 PAT == 1 print $0 $1 ~ /^|_/ PAT=0
whole line:
grep -A99999 -B2 address-info: INPUT.txt | awk 'BEGIN PAT=1 PAT == 1 print $0 $1 ~ /^|_/ PAT=0'
this in unacceptable in production mode as is it not universal (NOT working in (very unlikly, but possible) cases where
the number of lines "address-info:" AND "|_" is more than 99999), CPU/MEM unefficiant and untidy.
I'd like to have a way WITHIN the grep command to acheive this,
any ideas ?
grep regular-expression
add a comment |
I'm looking/searching for a way to make grep stop when finding the "|_" specific string after having found
the search string.
for example, i want to print ONLY the lines starting 2 lines above "address-info:" and stopping at line "|_"
AND NOT print "irrelevant lines in between" which BTW could by anything
INPUT:
Nmap scan report for ::1.2.3.4
Host script results:
| address-info:
| IPv4-compatible:
|_ IPv4 address: 1.2.3.4
...
irrelevant lines in between
irrelevant lines in between
irrelevant lines in between
...
Nmap scan report for ::ffff:1.2.3.4
Host script results:
| address-info:
| IPv4-mapped:
|_ IPv4 address: 1.2.3.4
...
irrelevant lines in between
irrelevant lines in between
irrelevant lines in between
...
Nmap scan report for 2001:0:506:708:282a:3d75:fefd:fcfb
Host script results:
| address-info:
| Teredo:
| Server IPv4 address: 5.6.7.8
| Client IPv4 address: 1.2.3.4
|_ UDP port: 49802
OUTPUT:
Nmap scan report for ::1.2.3.4
Host script results:
| address-info:
| IPv4-compatible:
|_ IPv4 address: 1.2.3.4
Nmap scan report for ::ffff:1.2.3.4
Host script results:
| address-info:
| IPv4-mapped:
|_ IPv4 address: 1.2.3.4
Nmap scan report for 2001:0:506:708:282a:3d75:fefd:fcfb
Host script results:
| address-info:
| Teredo:
| Server IPv4 address: 5.6.7.8
| Client IPv4 address: 1.2.3.4
|_ UDP port: 49802
I'v read man grep and found -A -B -C options, it's OK for -B as I know in advance how many line before but for -A
I gave an arbituray high value ie: 99999 in
grep -A99999 -B2 address-info: INPUT.txt
and piping in awk to find "|_"
awk 'BEGIN PAT=1 PAT == 1 print $0 $1 ~ /^|_/ PAT=0
whole line:
grep -A99999 -B2 address-info: INPUT.txt | awk 'BEGIN PAT=1 PAT == 1 print $0 $1 ~ /^|_/ PAT=0'
this in unacceptable in production mode as is it not universal (NOT working in (very unlikly, but possible) cases where
the number of lines "address-info:" AND "|_" is more than 99999), CPU/MEM unefficiant and untidy.
I'd like to have a way WITHIN the grep command to acheive this,
any ideas ?
grep regular-expression
2
Have you considered using the "grep
friendly" mode ofnmap
? For examplenmap -oG -
– roaima
Apr 14 at 13:56
add a comment |
I'm looking/searching for a way to make grep stop when finding the "|_" specific string after having found
the search string.
for example, i want to print ONLY the lines starting 2 lines above "address-info:" and stopping at line "|_"
AND NOT print "irrelevant lines in between" which BTW could by anything
INPUT:
Nmap scan report for ::1.2.3.4
Host script results:
| address-info:
| IPv4-compatible:
|_ IPv4 address: 1.2.3.4
...
irrelevant lines in between
irrelevant lines in between
irrelevant lines in between
...
Nmap scan report for ::ffff:1.2.3.4
Host script results:
| address-info:
| IPv4-mapped:
|_ IPv4 address: 1.2.3.4
...
irrelevant lines in between
irrelevant lines in between
irrelevant lines in between
...
Nmap scan report for 2001:0:506:708:282a:3d75:fefd:fcfb
Host script results:
| address-info:
| Teredo:
| Server IPv4 address: 5.6.7.8
| Client IPv4 address: 1.2.3.4
|_ UDP port: 49802
OUTPUT:
Nmap scan report for ::1.2.3.4
Host script results:
| address-info:
| IPv4-compatible:
|_ IPv4 address: 1.2.3.4
Nmap scan report for ::ffff:1.2.3.4
Host script results:
| address-info:
| IPv4-mapped:
|_ IPv4 address: 1.2.3.4
Nmap scan report for 2001:0:506:708:282a:3d75:fefd:fcfb
Host script results:
| address-info:
| Teredo:
| Server IPv4 address: 5.6.7.8
| Client IPv4 address: 1.2.3.4
|_ UDP port: 49802
I'v read man grep and found -A -B -C options, it's OK for -B as I know in advance how many line before but for -A
I gave an arbituray high value ie: 99999 in
grep -A99999 -B2 address-info: INPUT.txt
and piping in awk to find "|_"
awk 'BEGIN PAT=1 PAT == 1 print $0 $1 ~ /^|_/ PAT=0
whole line:
grep -A99999 -B2 address-info: INPUT.txt | awk 'BEGIN PAT=1 PAT == 1 print $0 $1 ~ /^|_/ PAT=0'
this in unacceptable in production mode as is it not universal (NOT working in (very unlikly, but possible) cases where
the number of lines "address-info:" AND "|_" is more than 99999), CPU/MEM unefficiant and untidy.
I'd like to have a way WITHIN the grep command to acheive this,
any ideas ?
grep regular-expression
I'm looking/searching for a way to make grep stop when finding the "|_" specific string after having found
the search string.
for example, i want to print ONLY the lines starting 2 lines above "address-info:" and stopping at line "|_"
AND NOT print "irrelevant lines in between" which BTW could by anything
INPUT:
Nmap scan report for ::1.2.3.4
Host script results:
| address-info:
| IPv4-compatible:
|_ IPv4 address: 1.2.3.4
...
irrelevant lines in between
irrelevant lines in between
irrelevant lines in between
...
Nmap scan report for ::ffff:1.2.3.4
Host script results:
| address-info:
| IPv4-mapped:
|_ IPv4 address: 1.2.3.4
...
irrelevant lines in between
irrelevant lines in between
irrelevant lines in between
...
Nmap scan report for 2001:0:506:708:282a:3d75:fefd:fcfb
Host script results:
| address-info:
| Teredo:
| Server IPv4 address: 5.6.7.8
| Client IPv4 address: 1.2.3.4
|_ UDP port: 49802
OUTPUT:
Nmap scan report for ::1.2.3.4
Host script results:
| address-info:
| IPv4-compatible:
|_ IPv4 address: 1.2.3.4
Nmap scan report for ::ffff:1.2.3.4
Host script results:
| address-info:
| IPv4-mapped:
|_ IPv4 address: 1.2.3.4
Nmap scan report for 2001:0:506:708:282a:3d75:fefd:fcfb
Host script results:
| address-info:
| Teredo:
| Server IPv4 address: 5.6.7.8
| Client IPv4 address: 1.2.3.4
|_ UDP port: 49802
I'v read man grep and found -A -B -C options, it's OK for -B as I know in advance how many line before but for -A
I gave an arbituray high value ie: 99999 in
grep -A99999 -B2 address-info: INPUT.txt
and piping in awk to find "|_"
awk 'BEGIN PAT=1 PAT == 1 print $0 $1 ~ /^|_/ PAT=0
whole line:
grep -A99999 -B2 address-info: INPUT.txt | awk 'BEGIN PAT=1 PAT == 1 print $0 $1 ~ /^|_/ PAT=0'
this in unacceptable in production mode as is it not universal (NOT working in (very unlikly, but possible) cases where
the number of lines "address-info:" AND "|_" is more than 99999), CPU/MEM unefficiant and untidy.
I'd like to have a way WITHIN the grep command to acheive this,
any ideas ?
grep regular-expression
grep regular-expression
edited Apr 14 at 13:55
Freddy
1,989210
1,989210
asked Apr 14 at 13:22
user2901196user2901196
243
243
2
Have you considered using the "grep
friendly" mode ofnmap
? For examplenmap -oG -
– roaima
Apr 14 at 13:56
add a comment |
2
Have you considered using the "grep
friendly" mode ofnmap
? For examplenmap -oG -
– roaima
Apr 14 at 13:56
2
2
Have you considered using the "
grep
friendly" mode of nmap
? For example nmap -oG -
– roaima
Apr 14 at 13:56
Have you considered using the "
grep
friendly" mode of nmap
? For example nmap -oG -
– roaima
Apr 14 at 13:56
add a comment |
2 Answers
2
active
oldest
votes
Use awk
directly with a range:
awk '/^Nmap scan report/;/^Host script results/,/|_/' INPUT.txt
grep
does not have any 'range' capabilities. But you can pipe the output of grep address-info: -B2 -A 99999
to it.
OK, all 3 work, thx
– user2901196
yesterday
add a comment |
Use grep
to match the start of the lines you want (test if that doesn't match the other lines):
grep -E '^(Nmap scan report for|Host script results:||[ _])' INPUT.txt
Or sed
and range pattern (similar to the awk solution)
sed -n '/^Nmap scan report for/,/^|_/p' INPUT.txt
- begin:
^Nmap scan report for
- end:
^|_
OK, all 3 work, thx
– user2901196
yesterday
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%2f512411%2fgrep-stop-regex-replacing-a-option%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
Use awk
directly with a range:
awk '/^Nmap scan report/;/^Host script results/,/|_/' INPUT.txt
grep
does not have any 'range' capabilities. But you can pipe the output of grep address-info: -B2 -A 99999
to it.
OK, all 3 work, thx
– user2901196
yesterday
add a comment |
Use awk
directly with a range:
awk '/^Nmap scan report/;/^Host script results/,/|_/' INPUT.txt
grep
does not have any 'range' capabilities. But you can pipe the output of grep address-info: -B2 -A 99999
to it.
OK, all 3 work, thx
– user2901196
yesterday
add a comment |
Use awk
directly with a range:
awk '/^Nmap scan report/;/^Host script results/,/|_/' INPUT.txt
grep
does not have any 'range' capabilities. But you can pipe the output of grep address-info: -B2 -A 99999
to it.
Use awk
directly with a range:
awk '/^Nmap scan report/;/^Host script results/,/|_/' INPUT.txt
grep
does not have any 'range' capabilities. But you can pipe the output of grep address-info: -B2 -A 99999
to it.
edited Apr 14 at 14:00
answered Apr 14 at 13:54
mosvymosvy
10.3k11238
10.3k11238
OK, all 3 work, thx
– user2901196
yesterday
add a comment |
OK, all 3 work, thx
– user2901196
yesterday
OK, all 3 work, thx
– user2901196
yesterday
OK, all 3 work, thx
– user2901196
yesterday
add a comment |
Use grep
to match the start of the lines you want (test if that doesn't match the other lines):
grep -E '^(Nmap scan report for|Host script results:||[ _])' INPUT.txt
Or sed
and range pattern (similar to the awk solution)
sed -n '/^Nmap scan report for/,/^|_/p' INPUT.txt
- begin:
^Nmap scan report for
- end:
^|_
OK, all 3 work, thx
– user2901196
yesterday
add a comment |
Use grep
to match the start of the lines you want (test if that doesn't match the other lines):
grep -E '^(Nmap scan report for|Host script results:||[ _])' INPUT.txt
Or sed
and range pattern (similar to the awk solution)
sed -n '/^Nmap scan report for/,/^|_/p' INPUT.txt
- begin:
^Nmap scan report for
- end:
^|_
OK, all 3 work, thx
– user2901196
yesterday
add a comment |
Use grep
to match the start of the lines you want (test if that doesn't match the other lines):
grep -E '^(Nmap scan report for|Host script results:||[ _])' INPUT.txt
Or sed
and range pattern (similar to the awk solution)
sed -n '/^Nmap scan report for/,/^|_/p' INPUT.txt
- begin:
^Nmap scan report for
- end:
^|_
Use grep
to match the start of the lines you want (test if that doesn't match the other lines):
grep -E '^(Nmap scan report for|Host script results:||[ _])' INPUT.txt
Or sed
and range pattern (similar to the awk solution)
sed -n '/^Nmap scan report for/,/^|_/p' INPUT.txt
- begin:
^Nmap scan report for
- end:
^|_
answered Apr 14 at 14:30
FreddyFreddy
1,989210
1,989210
OK, all 3 work, thx
– user2901196
yesterday
add a comment |
OK, all 3 work, thx
– user2901196
yesterday
OK, all 3 work, thx
– user2901196
yesterday
OK, all 3 work, thx
– user2901196
yesterday
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%2f512411%2fgrep-stop-regex-replacing-a-option%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
2
Have you considered using the "
grep
friendly" mode ofnmap
? For examplenmap -oG -
– roaima
Apr 14 at 13:56