I have a script

#! /bin/bash
awk -v pat="$1" '{ if(match($0, pat)) {print $0} }' "$3"
awk -v pat="$1" -v rep="$2" '{gsub(pat, rep); print $0}' "$3"

I would like to run it like

myscript '\.\.+' ' ' ./inputfile

where $1 is supposed to be a regex understandable by awk (GNU awk).

Specifying pat as a regex expression as arguments to gsub() and match() will not work. I was wondering how I can make it work? Thanks.


You’re going about things correctly, as far as passing variables is concerned. To make the expression work you need to double the backslashes, at least with Gawk:

myscript '\\.\\.+' ' ' ./inputfile

Note that you can simplify the AWK parts:

awk -v pat="$1" 'match($0, pat)' "$3"
awk -v pat="$1" -v rep="$2" '{gsub(pat, rep)} 1' "$3"
| improve this answer | |
  • Thanks. Yes my typo. (1) Could you explain the reason of doubling backslash? Is it correct that bash will remove the backslash in $1 in awk -v pat="$1"? Does it work if gsub() sees the pattern argument without any backslash? Does gsub() not require its pattern argument to be enclosed in /.../? (2) Is it possible to write a bash script, so that I can just specify a regex as an argument to the script in the same way as I specify it to awk directly? – Tim Nov 13 '18 at 14:56
  • Sorry, ignore partially my (1). I was thinking of something else. I need to specify literal dot to awk, so I need literal backslash to escape it. – Tim Nov 13 '18 at 15:22
  • (1) See the link in my updated answer re the double backslashes. Bash doesn’t remove the backslash, but AWK needs two because it processes them twice. gsub() doesn’t need /.../. (2) You could double the backslashes before calling AWK... – Stephen Kitt Nov 13 '18 at 15:49
  • notice that the behavior wrt. "\." is different between gawk and mawk (the default on debian); ex. echo foo.bar | awk '{gsub("\.","X")}1' will print different things depending on which awk was used. The values of variables set on the command line are treated exactly as if they were enclosed in ", and the standard leaves the behavior of awk wrt. non-standard escapes (other than \n,\t, etc) undefined. – mosvy Nov 13 '18 at 16:03

Just a quick answer with a different approach : script written in Awk:

#!/usr/bin/gawk -f
BEGIN { find=ARGV[1]; repl=ARGV[2]; delete ARGV[1]; delete ARGV[2]}
      { print gensub(find,repl,"g",$0) }

and then the usual Unix filter behavior:

$ chmod 755 myawkscript

$ ls |myawkscript '\w(.)' '{\1}'

$ myawkscript '(\w+)' '{\1}' myawkscript 
#!/{usr}/{bin}/{gawk} -{f}
{BEGIN} { {find}={ARGV}[{1}]; {repl}={ARGV}[{2}]; ..... 
      { {print} {gensub}({find},{repl},"{g}",${0}) }
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.