grep pattern before another pattern and print it all
Clash Royale CLAN TAG#URR8PPP
up vote
4
down vote
favorite
Given intput:
Via: 1.1.1.1
not relevant line
keyword + some text
...
not relevant line N
keyword + some text
...
not relevant line N
Via: 2.2.2.2
not relevant line
keyword + some text
...
not relevant line N
keyword + some text
...
not relevant line N
Via: 3.3.3.3
not relevant lines
Via: 4.4.4.4
not relevant
Via: 5.5.5.5
not relevant line
keyword + some text
...
not relevant line N
keyword + some text
...
not relevant line N
not relevant line N
...
Required output:
Via: 1.1.1.1
keyword + some text A
keyword + some text A
Via: 2.2.2.2
keyword + some text B
keyword + some text C
Via: 5.5.5.5
keyword + some text D
keyword + some text E
keyword string can occur N times in any Via block, or may not occur at all. In the output I need only those Via blocks where keyword occurs together with keyword strings belonging to them. The closest answer I found is here, but I can't make it into what I need.
text-processing awk sed grep
add a comment |Â
up vote
4
down vote
favorite
Given intput:
Via: 1.1.1.1
not relevant line
keyword + some text
...
not relevant line N
keyword + some text
...
not relevant line N
Via: 2.2.2.2
not relevant line
keyword + some text
...
not relevant line N
keyword + some text
...
not relevant line N
Via: 3.3.3.3
not relevant lines
Via: 4.4.4.4
not relevant
Via: 5.5.5.5
not relevant line
keyword + some text
...
not relevant line N
keyword + some text
...
not relevant line N
not relevant line N
...
Required output:
Via: 1.1.1.1
keyword + some text A
keyword + some text A
Via: 2.2.2.2
keyword + some text B
keyword + some text C
Via: 5.5.5.5
keyword + some text D
keyword + some text E
keyword string can occur N times in any Via block, or may not occur at all. In the output I need only those Via blocks where keyword occurs together with keyword strings belonging to them. The closest answer I found is here, but I can't make it into what I need.
text-processing awk sed grep
add a comment |Â
up vote
4
down vote
favorite
up vote
4
down vote
favorite
Given intput:
Via: 1.1.1.1
not relevant line
keyword + some text
...
not relevant line N
keyword + some text
...
not relevant line N
Via: 2.2.2.2
not relevant line
keyword + some text
...
not relevant line N
keyword + some text
...
not relevant line N
Via: 3.3.3.3
not relevant lines
Via: 4.4.4.4
not relevant
Via: 5.5.5.5
not relevant line
keyword + some text
...
not relevant line N
keyword + some text
...
not relevant line N
not relevant line N
...
Required output:
Via: 1.1.1.1
keyword + some text A
keyword + some text A
Via: 2.2.2.2
keyword + some text B
keyword + some text C
Via: 5.5.5.5
keyword + some text D
keyword + some text E
keyword string can occur N times in any Via block, or may not occur at all. In the output I need only those Via blocks where keyword occurs together with keyword strings belonging to them. The closest answer I found is here, but I can't make it into what I need.
text-processing awk sed grep
Given intput:
Via: 1.1.1.1
not relevant line
keyword + some text
...
not relevant line N
keyword + some text
...
not relevant line N
Via: 2.2.2.2
not relevant line
keyword + some text
...
not relevant line N
keyword + some text
...
not relevant line N
Via: 3.3.3.3
not relevant lines
Via: 4.4.4.4
not relevant
Via: 5.5.5.5
not relevant line
keyword + some text
...
not relevant line N
keyword + some text
...
not relevant line N
not relevant line N
...
Required output:
Via: 1.1.1.1
keyword + some text A
keyword + some text A
Via: 2.2.2.2
keyword + some text B
keyword + some text C
Via: 5.5.5.5
keyword + some text D
keyword + some text E
keyword string can occur N times in any Via block, or may not occur at all. In the output I need only those Via blocks where keyword occurs together with keyword strings belonging to them. The closest answer I found is here, but I can't make it into what I need.
text-processing awk sed grep
text-processing awk sed grep
edited Oct 1 '17 at 19:34
don_crissti
47k15124154
47k15124154
asked Oct 1 '17 at 19:09
Olga
150114
150114
add a comment |Â
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
5
down vote
accepted
With sed
:
sed -n '/^Via:/ x; /keyword/p; d; ; /keyword/H; $ x; /keyword/p; ' input.txt
Or, if you want keyword
anchored at the beginning of line:
sed -n '/^Via:/ x; /nkeyword/p; d; ; /^keyword/H; $ x; /nkeyword/p; ' input.txt
Many thanks @Sato Katsura
â Olga
Oct 1 '17 at 19:44
add a comment |Â
up vote
4
down vote
Awk solution:
awk '/^Via:/ f=1; r=$0; kw=0; next
f && /keyword/ printf "%s%sn",(!kw)? r ORS:"",$0; kw++ ' file
/^Via:/
- capturing line starting withVia:
intor
variable. Set flagf=1
into "active" state indicating processing of a certainVia
blockkw
- flag denoting the number of "keyword" lines under eachVia
blockf && /keyword/
- while processing lines underVia
block - consider only lines matchingkeyword
pattern
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
5
down vote
accepted
With sed
:
sed -n '/^Via:/ x; /keyword/p; d; ; /keyword/H; $ x; /keyword/p; ' input.txt
Or, if you want keyword
anchored at the beginning of line:
sed -n '/^Via:/ x; /nkeyword/p; d; ; /^keyword/H; $ x; /nkeyword/p; ' input.txt
Many thanks @Sato Katsura
â Olga
Oct 1 '17 at 19:44
add a comment |Â
up vote
5
down vote
accepted
With sed
:
sed -n '/^Via:/ x; /keyword/p; d; ; /keyword/H; $ x; /keyword/p; ' input.txt
Or, if you want keyword
anchored at the beginning of line:
sed -n '/^Via:/ x; /nkeyword/p; d; ; /^keyword/H; $ x; /nkeyword/p; ' input.txt
Many thanks @Sato Katsura
â Olga
Oct 1 '17 at 19:44
add a comment |Â
up vote
5
down vote
accepted
up vote
5
down vote
accepted
With sed
:
sed -n '/^Via:/ x; /keyword/p; d; ; /keyword/H; $ x; /keyword/p; ' input.txt
Or, if you want keyword
anchored at the beginning of line:
sed -n '/^Via:/ x; /nkeyword/p; d; ; /^keyword/H; $ x; /nkeyword/p; ' input.txt
With sed
:
sed -n '/^Via:/ x; /keyword/p; d; ; /keyword/H; $ x; /keyword/p; ' input.txt
Or, if you want keyword
anchored at the beginning of line:
sed -n '/^Via:/ x; /nkeyword/p; d; ; /^keyword/H; $ x; /nkeyword/p; ' input.txt
answered Oct 1 '17 at 19:27
Satà  Katsura
10.7k11533
10.7k11533
Many thanks @Sato Katsura
â Olga
Oct 1 '17 at 19:44
add a comment |Â
Many thanks @Sato Katsura
â Olga
Oct 1 '17 at 19:44
Many thanks @Sato Katsura
â Olga
Oct 1 '17 at 19:44
Many thanks @Sato Katsura
â Olga
Oct 1 '17 at 19:44
add a comment |Â
up vote
4
down vote
Awk solution:
awk '/^Via:/ f=1; r=$0; kw=0; next
f && /keyword/ printf "%s%sn",(!kw)? r ORS:"",$0; kw++ ' file
/^Via:/
- capturing line starting withVia:
intor
variable. Set flagf=1
into "active" state indicating processing of a certainVia
blockkw
- flag denoting the number of "keyword" lines under eachVia
blockf && /keyword/
- while processing lines underVia
block - consider only lines matchingkeyword
pattern
add a comment |Â
up vote
4
down vote
Awk solution:
awk '/^Via:/ f=1; r=$0; kw=0; next
f && /keyword/ printf "%s%sn",(!kw)? r ORS:"",$0; kw++ ' file
/^Via:/
- capturing line starting withVia:
intor
variable. Set flagf=1
into "active" state indicating processing of a certainVia
blockkw
- flag denoting the number of "keyword" lines under eachVia
blockf && /keyword/
- while processing lines underVia
block - consider only lines matchingkeyword
pattern
add a comment |Â
up vote
4
down vote
up vote
4
down vote
Awk solution:
awk '/^Via:/ f=1; r=$0; kw=0; next
f && /keyword/ printf "%s%sn",(!kw)? r ORS:"",$0; kw++ ' file
/^Via:/
- capturing line starting withVia:
intor
variable. Set flagf=1
into "active" state indicating processing of a certainVia
blockkw
- flag denoting the number of "keyword" lines under eachVia
blockf && /keyword/
- while processing lines underVia
block - consider only lines matchingkeyword
pattern
Awk solution:
awk '/^Via:/ f=1; r=$0; kw=0; next
f && /keyword/ printf "%s%sn",(!kw)? r ORS:"",$0; kw++ ' file
/^Via:/
- capturing line starting withVia:
intor
variable. Set flagf=1
into "active" state indicating processing of a certainVia
blockkw
- flag denoting the number of "keyword" lines under eachVia
blockf && /keyword/
- while processing lines underVia
block - consider only lines matchingkeyword
pattern
edited Oct 1 '17 at 20:01
answered Oct 1 '17 at 19:42
RomanPerekhrest
22.5k12145
22.5k12145
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%2f395509%2fgrep-pattern-before-another-pattern-and-print-it-all%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