I'm trying to use regular expression in awk command, where this regular expression should be a variable, and I could change it before calling the awk command
Example:
gawk 'BEGIN{RS=ORS="\n\n" {s=tolower($0)} s~/word1|word2/' file1.log
this worked for me, this commands prints all lines contains words1 or word2 from last "new line" to the next "new line"
What I want to do is, putting word1 and word2 in a variable and use it in gawk
for example:
regex="word1|word2"
gawk 'BEGIN{RS=ORS="\n\n" {s=tolower($0)} s~/$regex/' file1.log
where regex can be changed depends on my program I searched and tried many solutions but nothing worked for me
Solutions I tried:
1) regex="word1|word2";
gawk 'BEGIN{RS=ORS="\n\n" {s=tolower($0)} s~/$regex/' file1.log
2) regex="word1|word2"
gawk -v pat="$regex" 'BEGIN{RS=ORS="\n\n" {s=tolower($0)} s~/pat/' file1.log
3) regex="word1|word2"
gawk 'BEGIN{RS=ORS="\n\n" {s=tolower($0)} {pat=$regex} s~/pat/' file1.log
4) regex="word1|word2"
echo $regex | gawk 'BEGIN{RS=ORS="\n\n" {s=tolower($0)} s~/$1/' file1.log
Thanks in advance!