4

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.

5

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
4

Awk solution:

awk '/^Via:/{ f=1; r=$0; kw=0; next }
     f && /keyword/{ printf "%s%s\n",(!kw)? r ORS:"",$0; kw++ }' file

  • /^Via:/ - capturing line starting with Via: into r variable. Set flag f=1 into "active" state indicating processing of a certain Via block

  • kw - flag denoting the number of "keyword" lines under each Via block

  • f && /keyword/ - while processing lines under Via block - consider only lines matching keyword pattern

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.