Skip to main content
added 521 characters in body
Source Link
Uwe
  • 3.4k
  • 20
  • 20

If you know that src: occurs at the beginning of a line and that foo is enclosed in quotes and preceded by a space and that there must be a colon earlier in the line, use

awk 'BEGIN{a=0} /^$/{if(a==1) print b; a=0} /:.* "foo"/{a=1} /^src:/{b=$0} END{if(a==1) print b}' 

We use the variable a to remember whether or not the pattern foo occurs in the input block, and the variable b to store the src: line. At the beginning, a is set to 0. Whenever we find an empty line (i.e., ^$), we check the value of a, conditionally print b, and reset a. If we encounter "foo" preceded by a colon earlier in the line, we set a to 1. If we encounter src: at the beginning of a line (^), we store it in b. At the end, we check once more whether a == 1, if so, we print b.

If you know that src: occurs at the beginning of a line and that foo is enclosed in quotes and preceded by a space and that there must be a colon earlier in the line, use

awk 'BEGIN{a=0} /^$/{if(a==1) print b; a=0} /:.* "foo"/{a=1} /^src:/{b=$0} END{if(a==1) print b}' 

If you know that src: occurs at the beginning of a line and that foo is enclosed in quotes and preceded by a space and that there must be a colon earlier in the line, use

awk 'BEGIN{a=0} /^$/{if(a==1) print b; a=0} /:.* "foo"/{a=1} /^src:/{b=$0} END{if(a==1) print b}' 

We use the variable a to remember whether or not the pattern foo occurs in the input block, and the variable b to store the src: line. At the beginning, a is set to 0. Whenever we find an empty line (i.e., ^$), we check the value of a, conditionally print b, and reset a. If we encounter "foo" preceded by a colon earlier in the line, we set a to 1. If we encounter src: at the beginning of a line (^), we store it in b. At the end, we check once more whether a == 1, if so, we print b.

deleted 198 characters in body
Source Link
Uwe
  • 3.4k
  • 20
  • 20

Simple version:

awk 'BEGIN{a=0} /foo/{a=1} /src:/{b=$0} END{if(a==1) print b}' 

If you know that src: occurs at the beginning of a line and that foo is enclosed in quotes and preceded by a space and that there must be a colon earlier in the line, use

awk 'BEGIN{a=0} /^$/{if(a==1) print b; a=0} /:.* "foo"/{a=1} /^src:/{b=$0} END{if(a==1) print b}' 

One can also do it with sed, but this is fairly unreadable:

sed -ne '1{x;s/^/ /;x;};/foo/{x;s/^ /X/;x;};/src:/H;${x;s/^X\n//p;}' 

Simple version:

awk 'BEGIN{a=0} /foo/{a=1} /src:/{b=$0} END{if(a==1) print b}' 

If you know that src: occurs at the beginning of a line and that foo is enclosed in quotes and preceded by a space and that there must be a colon earlier in the line, use

awk 'BEGIN{a=0} /:.* "foo"/{a=1} /^src:/{b=$0} END{if(a==1) print b}' 

One can also do it with sed, but this is fairly unreadable:

sed -ne '1{x;s/^/ /;x;};/foo/{x;s/^ /X/;x;};/src:/H;${x;s/^X\n//p;}' 

If you know that src: occurs at the beginning of a line and that foo is enclosed in quotes and preceded by a space and that there must be a colon earlier in the line, use

awk 'BEGIN{a=0} /^$/{if(a==1) print b; a=0} /:.* "foo"/{a=1} /^src:/{b=$0} END{if(a==1) print b}' 
added 141 characters in body
Source Link
Uwe
  • 3.4k
  • 20
  • 20

Simple version:

awk 'BEGIN{a=0} /foo/{a=1} /src:/{b=$0} END{if(a==1) print b}' 

If you know that src: occurs at the beginning of a line and that foo is enclosed in quotes and preceded by a space and that there must be a colon earlier in the line, use

awk 'BEGIN{a=0} /:.* "foo"/{a=1} /^src:/{b=$0} END{if(a==1) print b}' 

One can also do it with sed, but this is fairly unreadable:

sed -ne '1{x;s/^/ /;x;};/foo/{x;s/^ /X/;x;};/src:/H;${x;s/^X\n//p;}' 

Simple version:

awk 'BEGIN{a=0} /foo/{a=1} /src:/{b=$0} END{if(a==1) print b}' 

If you know that src: occurs at the beginning of a line and that foo is enclosed in quotes and preceded by a space and that there must be a colon earlier in the line, use

awk 'BEGIN{a=0} /:.* "foo"/{a=1} /^src:/{b=$0} END{if(a==1) print b}' 

Simple version:

awk 'BEGIN{a=0} /foo/{a=1} /src:/{b=$0} END{if(a==1) print b}' 

If you know that src: occurs at the beginning of a line and that foo is enclosed in quotes and preceded by a space and that there must be a colon earlier in the line, use

awk 'BEGIN{a=0} /:.* "foo"/{a=1} /^src:/{b=$0} END{if(a==1) print b}' 

One can also do it with sed, but this is fairly unreadable:

sed -ne '1{x;s/^/ /;x;};/foo/{x;s/^ /X/;x;};/src:/H;${x;s/^X\n//p;}' 
added 270 characters in body
Source Link
Uwe
  • 3.4k
  • 20
  • 20
Loading
Source Link
Uwe
  • 3.4k
  • 20
  • 20
Loading