grep: detect multi-line pattern with double capture
Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
I have file, say test.s that contains several trivial infinite loops:
.LBB7_7:
branch .LBB7_7
Labels may be totally different, but all are like .LBBd_d+
I want some neat way to process such things with grep or sed one-liner.
Now I am doing this way. First I calculate all labels:
grep -oP 'branch .KLBBd_d+' minimize.s
And then in bash for loop I looking up label with one line below with grep -A1 "^.$i:"
and checking its output for branch $i
Can I do better (without explicit bash processing with grep only)?
text-processing grep
add a comment |Â
up vote
1
down vote
favorite
I have file, say test.s that contains several trivial infinite loops:
.LBB7_7:
branch .LBB7_7
Labels may be totally different, but all are like .LBBd_d+
I want some neat way to process such things with grep or sed one-liner.
Now I am doing this way. First I calculate all labels:
grep -oP 'branch .KLBBd_d+' minimize.s
And then in bash for loop I looking up label with one line below with grep -A1 "^.$i:"
and checking its output for branch $i
Can I do better (without explicit bash processing with grep only)?
text-processing grep
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I have file, say test.s that contains several trivial infinite loops:
.LBB7_7:
branch .LBB7_7
Labels may be totally different, but all are like .LBBd_d+
I want some neat way to process such things with grep or sed one-liner.
Now I am doing this way. First I calculate all labels:
grep -oP 'branch .KLBBd_d+' minimize.s
And then in bash for loop I looking up label with one line below with grep -A1 "^.$i:"
and checking its output for branch $i
Can I do better (without explicit bash processing with grep only)?
text-processing grep
I have file, say test.s that contains several trivial infinite loops:
.LBB7_7:
branch .LBB7_7
Labels may be totally different, but all are like .LBBd_d+
I want some neat way to process such things with grep or sed one-liner.
Now I am doing this way. First I calculate all labels:
grep -oP 'branch .KLBBd_d+' minimize.s
And then in bash for loop I looking up label with one line below with grep -A1 "^.$i:"
and checking its output for branch $i
Can I do better (without explicit bash processing with grep only)?
text-processing grep
asked Dec 14 '17 at 9:11
Konstantin Vladimirov
1084
1084
add a comment |Â
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
3
down vote
accepted
try this:
$ cat ip.txt
foo
baz
.LBB7_7:
branch .LBB7_7
xyzyadsf
.LBB8_3:
mov a, b
branch .LBB8_3
nop
$ grep -zoP '(.LBBd_d+):s*branchh+1n' ip.txt
.LBB7_7:
branch .LBB7_7
-z
will use ASCII NUL as record separator instead of default newline character. Assuming your input doesn't have NUL characters, this will cause whole file to be slurped(.LBBd_d+)
capture label, but can't specify to match at start of line:s*branchh+1n
condition to check for infinite loop
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
try this:
$ cat ip.txt
foo
baz
.LBB7_7:
branch .LBB7_7
xyzyadsf
.LBB8_3:
mov a, b
branch .LBB8_3
nop
$ grep -zoP '(.LBBd_d+):s*branchh+1n' ip.txt
.LBB7_7:
branch .LBB7_7
-z
will use ASCII NUL as record separator instead of default newline character. Assuming your input doesn't have NUL characters, this will cause whole file to be slurped(.LBBd_d+)
capture label, but can't specify to match at start of line:s*branchh+1n
condition to check for infinite loop
add a comment |Â
up vote
3
down vote
accepted
try this:
$ cat ip.txt
foo
baz
.LBB7_7:
branch .LBB7_7
xyzyadsf
.LBB8_3:
mov a, b
branch .LBB8_3
nop
$ grep -zoP '(.LBBd_d+):s*branchh+1n' ip.txt
.LBB7_7:
branch .LBB7_7
-z
will use ASCII NUL as record separator instead of default newline character. Assuming your input doesn't have NUL characters, this will cause whole file to be slurped(.LBBd_d+)
capture label, but can't specify to match at start of line:s*branchh+1n
condition to check for infinite loop
add a comment |Â
up vote
3
down vote
accepted
up vote
3
down vote
accepted
try this:
$ cat ip.txt
foo
baz
.LBB7_7:
branch .LBB7_7
xyzyadsf
.LBB8_3:
mov a, b
branch .LBB8_3
nop
$ grep -zoP '(.LBBd_d+):s*branchh+1n' ip.txt
.LBB7_7:
branch .LBB7_7
-z
will use ASCII NUL as record separator instead of default newline character. Assuming your input doesn't have NUL characters, this will cause whole file to be slurped(.LBBd_d+)
capture label, but can't specify to match at start of line:s*branchh+1n
condition to check for infinite loop
try this:
$ cat ip.txt
foo
baz
.LBB7_7:
branch .LBB7_7
xyzyadsf
.LBB8_3:
mov a, b
branch .LBB8_3
nop
$ grep -zoP '(.LBBd_d+):s*branchh+1n' ip.txt
.LBB7_7:
branch .LBB7_7
-z
will use ASCII NUL as record separator instead of default newline character. Assuming your input doesn't have NUL characters, this will cause whole file to be slurped(.LBBd_d+)
capture label, but can't specify to match at start of line:s*branchh+1n
condition to check for infinite loop
answered Dec 14 '17 at 9:24
Sundeep
6,9511826
6,9511826
add a comment |Â
add a comment |Â
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
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f410824%2fgrep-detect-multi-line-pattern-with-double-capture%23new-answer', 'question_page');
);
Post as a guest
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
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
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