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;








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 ?










share|improve this question



















  • 2





    Have you considered using the "grep friendly" mode of nmap? For example nmap -oG -

    – roaima
    Apr 14 at 13:56


















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 ?










share|improve this question



















  • 2





    Have you considered using the "grep friendly" mode of nmap? For example nmap -oG -

    – roaima
    Apr 14 at 13:56














0












0








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 ?










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 of nmap? For example nmap -oG -

    – roaima
    Apr 14 at 13:56













  • 2





    Have you considered using the "grep friendly" mode of nmap? For example nmap -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











2 Answers
2






active

oldest

votes


















0














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.






share|improve this answer

























  • OK, all 3 work, thx

    – user2901196
    yesterday


















0














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: ^|_





share|improve this answer























  • OK, all 3 work, thx

    – user2901196
    yesterday











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
);



);













draft saved

draft discarded


















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









0














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.






share|improve this answer

























  • OK, all 3 work, thx

    – user2901196
    yesterday















0














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.






share|improve this answer

























  • OK, all 3 work, thx

    – user2901196
    yesterday













0












0








0







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.






share|improve this answer















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.







share|improve this answer














share|improve this answer



share|improve this answer








edited Apr 14 at 14:00

























answered Apr 14 at 13:54









mosvymosvy

10.3k11238




10.3k11238












  • OK, all 3 work, thx

    – user2901196
    yesterday

















  • OK, all 3 work, thx

    – user2901196
    yesterday
















OK, all 3 work, thx

– user2901196
yesterday





OK, all 3 work, thx

– user2901196
yesterday













0














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: ^|_





share|improve this answer























  • OK, all 3 work, thx

    – user2901196
    yesterday















0














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: ^|_





share|improve this answer























  • OK, all 3 work, thx

    – user2901196
    yesterday













0












0








0







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: ^|_





share|improve this answer













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: ^|_






share|improve this answer












share|improve this answer



share|improve this answer










answered Apr 14 at 14:30









FreddyFreddy

1,989210




1,989210












  • OK, all 3 work, thx

    – user2901196
    yesterday

















  • OK, all 3 work, thx

    – user2901196
    yesterday
















OK, all 3 work, thx

– user2901196
yesterday





OK, all 3 work, thx

– user2901196
yesterday

















draft saved

draft discarded
















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

getting Checkpoint VPN SSL Network Extender working in the command lineHow to connect to CheckPoint VPN on Ubuntu 18.04LTS?Will the Linux ( red-hat ) Open VPNC Client connect to checkpoint or nortel VPN gateways?VPN client for linux machine + support checkpoint gatewayVPN SSL Network Extender in FirefoxLinux Checkpoint SNX tool configuration issuesCheck Point - Connect under Linux - snx + OTPSNX VPN Ububuntu 18.XXUsing Checkpoint VPN SSL Network Extender CLI with certificateVPN with network manager (nm-applet) is not workingWill the Linux ( red-hat ) Open VPNC Client connect to checkpoint or nortel VPN gateways?VPN client for linux machine + support checkpoint gatewayImport VPN config files to NetworkManager from command lineTrouble connecting to VPN using network-manager, while command line worksStart a VPN connection with PPTP protocol on command linestarting a docker service daemon breaks the vpn networkCan't connect to vpn with Network-managerVPN SSL Network Extender in FirefoxUsing Checkpoint VPN SSL Network Extender CLI with certificate

Cannot Extend partition with GParted The 2019 Stack Overflow Developer Survey Results Are In Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) 2019 Community Moderator Election ResultsCan't increase partition size with GParted?GParted doesn't recognize the unallocated space after my current partitionWhat is the best way to add unallocated space located before to Ubuntu 12.04 partition with GParted live?I can't figure out how to extend my Arch home partition into free spaceGparted Linux Mint 18.1 issueTrying to extend but swap partition is showing as Unknown in Gparted, shows proper from fdiskRearrange partitions in gparted to extend a partitionUnable to extend partition even though unallocated space is next to it using GPartedAllocate free space to root partitiongparted: how to merge unallocated space with a partition

Marilyn Monroe Ny fiainany manokana | Jereo koa | Meny fitetezanafanitarana azy.