Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change regex delimiter in awk patterns

Tags:

regex

awk

gawk

nawk

Is is possible to change the default regex delimiter (slash) to other characters?

I tried to do it using with sed syntax but it didn't work.

$ gawk '\|bash| { print } ' backup.sh
gawk: |bash| { print }
gawk: ^ syntax error

The regex I am trying has many slashes. Escaping all of them will make it ugly and unreadable. I tried changing the / to | but it didn't work.

TIA

like image 705
Anvesh Avatar asked May 12 '12 10:05

Anvesh


1 Answers

AWK doesn't support that. Use a variable instead.

gawk 'BEGIN {pattern = "/"} $0 ~ pattern {print}' backup.sh
like image 175
Dennis Williamson Avatar answered Oct 31 '22 20:10

Dennis Williamson