gawk 模式的正则表达式

gawk regular expression for a pattern

我正在尝试使用 gawk 为以下表达式匹配模式:

static char *nm = "This is a test with many characters like $@#^&";

规律如下:

1. Line starts with static
2. 0 or more number of characters including whitespaces
3. char
4. 0 or more number of characters including whitespaces
5. =
6. 0 or more number of characters including whitespaces
7. "
8. 0 or more number of characters including whitespaces
9. "
10. 0 or more number of characters including whitespaces
11. ;

我正在尝试 gawk /^static.*char.*=.*\",*\";"/{print} 我试图在第一个匹配的模式之后插入包含 "bbbbb" 的行,尝试如下:

   gawk '1; ==nm1 && ++a==1 {print nm2}' nm1="^static.*char.*=.*\",*\";" nm2="bbbbb"  "aa"

如果再出现static char *nm = "This is a test with many characters like $@#^&";这将保持原样。

请帮忙。

==nm1 不会应用正则表达式,因为它只会对 </code> 而不是整个记录执行字符串相等性测试。</p> <p>您可以使用这个 awk 命令:</p> <pre><code>awk -v nm2="bbbbb" '1; !p && /^static.*char.*=.*"[^"]*";/{p++; print nm2}' file

如果您想使用参数传递正则表达式,请使用:

awk -v nm2="bbbbb" -v nm1='^static.*char.*=.*"[^"]*";' '1; !p && [=11=] ~ nm1{p++; print nm2}' file