How do I find the text that matches a pattern?

While on the surface this seems to be a simple question with an obvious answer it actually is not because of 2 factors: The word pattern is ambiguous – we don’t know if the OP wants to do a regexp match or a string match, and The word match is ambiguous – we don’t know … Read more

What are NR and FNR and what does “NR==FNR” imply?

In awk, FNR refers to the record number (typically the line number) in the current file, NR refers to the total record number. The operator == is a comparison operator, which returns true when the two surrounding operands are equal. This means that the condition NR==FNR is only true for the first file, as FNR … Read more

Printing with sed or awk a line following a matching pattern

Never use the word “pattern” as is it highly ambiguous. Always use “string” or “regexp” (or in shell “globbing pattern”), whichever it is you really mean. The specific answer you want is: awk ‘f{print;f=0} /regexp/{f=1}’ file or specializing the more general solution of the Nth record after a regexp (idiom “c” below): awk ‘c&&!–c; /regexp/{c=1}’ … Read more