-2

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!

Saeed isa
  • 330
  • 3
  • 18

2 Answers2

3

Could you please try following and let me know if this helps you.

If you want to keep both the strings into a single variable and want to use as a regex in awk then following may help you on same too.

regex='word1|word2'
awk -v reg="$regex" 'tolower($0) ~ reg' Input_file

There are few points here too:

I- Since you haven't provided any data Input_file sample so couldn't test it but my gut feeling is it should work.

II- Then you have mentioned RS and ORS to \n in BEGIN section of awk, which is redundant as by default itself they are set to new line so you could remove that part from code.

RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
  • Hi, This worked, but I want to use 1 variable that I can config before the awk command, since it could be in the next iteration word1, word2,word3 and word4 – Saeed isa Sep 24 '17 at 08:49
  • check my edit once, I have added variable from shell to awk which could have multiple strings in it. Let me know if this helps you. – RavinderSingh13 Sep 24 '17 at 08:49
  • 1
    @EdMorton, Apologies sir, I changed it to small letters now variable named to reg now. Thanks a TON for letting me know on same again. – RavinderSingh13 Sep 24 '17 at 13:19
0

If you want to input it as a compound expression you could also do this one I guess:

awk -v pat="$regex" '{split(regex, a, "|")}tolower($0) ~ a[1] || tolower($0) ~ a[2]'

Assuming your regex input of: regex="word1|word2"

JFS31
  • 518
  • 5
  • 13