Search code examples
regexprintingawkrecordseparator

print out found result of regexp in RS


Hi all can you help me print out result of regexp in RS ?

awk 'BEGIN {RS="--[0-9a-zA-Z]+--"} /pattern/' awk-test.txt

pattern="--[0-9a-zA-Z]+--" can be equal --95700e1b-- or --0dcaf754-- or else awk print out block of text between RS, how can I print found RS too?

PS sorry for my bad English.


Solution

  • Since RS can only be a single character in a portable awk script, I'll assume you are using gawk. In that case, you can reference RT, which will contain the text that matched the regular expression specified in RS. (RT is the record terminator, so it is the value of the text that matches RS at the end of the current record rather than the beginning.)