How to avoid the leaning toothpick syndrome in awk

awkregular expression

When matching patterns with / in them, having to escape the / quickly becomes unwieldy, ugly:

/\/usr\/share\/man\//

With sed, perl or Vim, I would use a different delimiter for the regex, say ::

sed '\:/usr/share/man/: do something'
perl -ne 'print if m:/usr/share/man/:'
:g:/usr/share/man/: do something " Vim

How can I avoid this awk? Something like:

awk ':/usr/share/man/: {do something}'

The best I can think of is to use a variable:

awk -v pat='/usr/share/man/' '$0 ~ pat {do something}'

But that is very verbose compared to the sed/perl/vim method.


Of course, there might be other ways to match paths like /usr/share/man/, but that's not the only place where / could appear in a pattern.

Best Answer

Just change /testregexp/ with $0 ~ "testregexp"

Simple exemple:

$ echo "a/b/c" | awk ' ( $0 ~ "a/b/c" ) { print "we have a winner" ; }'
we have a winner

another exemple

regexp='some regexp "with /lots/ of double quotes" "everywhere"'
awk -v reg="${regexp}" ' ( $0 ~ reg ) { action here... }'