While I have this awk script,
/regex2/{
var = $1}
/regex1/{
print var}
which I executed over input file:
regex1
This should be assigned as variable regex2
I got no printed output. The desired output is: “This” to be printed out.
I might then think to utilize BEGIN:
BEGIN{
/regex2/
var = $1}
/regex1/{
print var}
But apparently BEGIN cannot accommodate regex matching function. Any suggestion to this?
This would achieve the desired result:
Otherwise, you’ll need to read the file twice and perform something like the following. It will store the last occurrence of
/regex2/invar. Upon re-reading the file, it will printvarfor each occurrence of/regex1/. Note that you’ll get an empty line in the output and the keyword ‘This’ on a newline: