7

Consider this file:

#!/usr/bin/env bash
cat > example_file.txt <<EOL
|dms,wew,qwqw|
|m|head1|
|3,4,6|
|3e,2,23|
|m|head2|
|xw,12,2123|
|23,d,213|
|23,22q,2skl|
|m|head1|
|dljs,wqpw,2;a|
|dllw,w1p,1q;a|
EOL

Every once in a while, there will be a guard pattern: a line that starts with:

|m|

(yes, the first line of the file need not be a guard pattern) Now I only want to print those lines for which the last guard pattern was:

|m|head1|

and ignore all other lines. In other worst, I would like the output to be:

#!/usr/bin/env bash
cat > desired_result_file.txt <<EOL
|m|head1|
|3,4,6|
|3e,2,23|
|m|head1|
|dljs,wqpw,2;a|
|dllw,w1p,1q;a|
EOL
7

Regex-oriented

$ awk '/^\|m\|/ {/\|head1\|/ ? p=1 : p=0} p' example_file.txt 
|m|head1|
|3,4,6|
|3e,2,23|
|m|head1|
|dljs,wqpw,2;a|
|dllw,w1p,1q;a|

or field-oriented

$ awk -F'|' '$2 == "m" {$3 == "head1" ? p=1 : p=0} p' example_file.txt 
|m|head1|
|3,4,6|
|3e,2,23|
|m|head1|
|dljs,wqpw,2;a|
|dllw,w1p,1q;a|

p is effectively a print flag.

Awk programs consist of pattern {action} pairs, in which action is executed on a record if pattern evaluates true (non-zero). You can omit pattern - in which case {action} is evaluated for every record - or omit {action} in which case awk applies the default pattern, which is to print the record: the latter is what's happening here.

  • How does the variable p work to control printing? I see how it is set, but where is that explained in the awk manual, etc.? – Joe Sep 26 at 8:03
  • @Joe I have added some explanation of the p – steeldriver Sep 26 at 11:51

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.