6

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

Anvesh
  • 395
  • 1
  • 2
  • 10

1 Answers1

2

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

gawk 'BEGIN {pattern = "/"} $0 ~ pattern {print}' backup.sh
Dennis Williamson
  • 346,391
  • 90
  • 374
  • 439
  • I am trying to use this example when extracting lines between 2 patterns, eg `awk '/pat1/,/pat2/' foo.html` -- patterns contain both slashes and double quote chars (it is html code). Can you provide an example showing to do this without having to escape the slashes and double quotes? – Jonathan Cross Jul 05 '19 at 11:42
  • @JonathanCross it's not possible without doing some escaping somewhere. You may need to create a new question showing the specific issues you're dealing with. – Dennis Williamson Jul 05 '19 at 12:42
  • Thanks. Perhaps you can show how `awk '/pat1/,/pat2/' foo.html` would be adapted to use 2 vars instead of `/pat1/` and `/pat2/` -- and then I can figure out the escaping? – Jonathan Cross Jul 05 '19 at 13:44
  • 1
    @JonathanCross: `awk 'BEGIN {var1 = "pat1"; var2 = "pat2"} $0 ~ var1, $0 ~ var2 {print}' filename` is one way. Another: `awk -v var1='pat1' -v var2='pat2' '$0 ~ var1, $0 ~ var2 {print}' filename` is another (which allows you to pass in shell variables as well as the strings/patterns shown). – Dennis Williamson Jul 06 '19 at 02:52