简体   繁体   English

以awk模式更改正则表达式分隔符

[英]Change regex delimiter in awk patterns

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. 我尝试使用sed语法来实现它,但它没有用。

$ 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 TIA

AWK doesn't support that. AWK不支持这一点。 Use a variable instead. 改为使用变量。

gawk 'BEGIN {pattern = "/"} $0 ~ pattern {print}' backup.sh

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM