为什么 awk 似乎无法根据涉及“。”的替代方案拆分为多个字段?
why doesn't awk seem to work on splitting into fields based on alternative involving "."?
在 .
:
上进行 awk 拆分是可以的
>printf foo.bar | awk '{split([=10=], a, "."); print a[1]}'
foo
awk split 可以替代:
>printf foo.bar | awk '{split([=11=], a, "b|a"); print a[1]}'
foo.
那为什么不能拆分涉及 .
的备选方案:
>printf foo.bar | awk '{split([=12=], a, ".|a"); print a[1]}'
(未打印)
逃离那个时期,我想你会成为黄金:
printf foo.bar | awk '{split([=10=], a, "\.|a"); print a[1]}'
JNevill 展示了如何让它工作。但是要回答你为什么在一种情况下需要转义而不是另一种情况的问题,我们可以在 "how fields are split, based on the value of FS." 的摘要中的 awk 手册中找到答案(同样的规则适用于 fieldsep 赋予 split
命令。)
最重要的是,当 FS 是单个字符时,它不会被视为正则表达式,否则就是。
因此 split([=12=], a, ".")
如我们希望的那样工作,将 .
字面上的 .
,但 split([=15=], a, ".|a")
将 .|a
视为正则表达式,其中 .
有特殊含义,将分隔符设置为 任何字符 ,因此必须添加反斜杠才能按字面意思处理 .
。
FS == " "
Fields are separated by runs of whitespace. Leading and trailing whitespace are ignored. This is the default.
FS == any single character
Fields are separated by each occurrence of the character. Multiple successive occurrences delimit empty fields, as do leading and
trailing occurrences.
FS == regexp
Fields are separated by occurrences of characters that match regexp. Leading and trailing matches of regexp delimit empty fields.
你可以看到尽管结果为空 .|a
确实在做一些事情,将行分成八个空字段 --- 与 ,,,,,,,
这样的行与 FS
设置为 ,
.
$ printf foo.bar | awk '{split([=10=], a, ".|a"); for (i in a) print i ": " a[i]; }'
4:
5:
6:
7:
8:
1:
2:
3:
在 .
:
>printf foo.bar | awk '{split([=10=], a, "."); print a[1]}'
foo
awk split 可以替代:
>printf foo.bar | awk '{split([=11=], a, "b|a"); print a[1]}'
foo.
那为什么不能拆分涉及 .
的备选方案:
>printf foo.bar | awk '{split([=12=], a, ".|a"); print a[1]}'
(未打印)
逃离那个时期,我想你会成为黄金:
printf foo.bar | awk '{split([=10=], a, "\.|a"); print a[1]}'
JNevill 展示了如何让它工作。但是要回答你为什么在一种情况下需要转义而不是另一种情况的问题,我们可以在 "how fields are split, based on the value of FS." 的摘要中的 awk 手册中找到答案(同样的规则适用于 fieldsep 赋予 split
命令。)
最重要的是,当 FS 是单个字符时,它不会被视为正则表达式,否则就是。
因此 split([=12=], a, ".")
如我们希望的那样工作,将 .
字面上的 .
,但 split([=15=], a, ".|a")
将 .|a
视为正则表达式,其中 .
有特殊含义,将分隔符设置为 任何字符 ,因此必须添加反斜杠才能按字面意思处理 .
。
FS == " "
Fields are separated by runs of whitespace. Leading and trailing whitespace are ignored. This is the default.FS == any single character
Fields are separated by each occurrence of the character. Multiple successive occurrences delimit empty fields, as do leading and trailing occurrences.FS == regexp
Fields are separated by occurrences of characters that match regexp. Leading and trailing matches of regexp delimit empty fields.
你可以看到尽管结果为空 .|a
确实在做一些事情,将行分成八个空字段 --- 与 ,,,,,,,
这样的行与 FS
设置为 ,
.
$ printf foo.bar | awk '{split([=10=], a, ".|a"); for (i in a) print i ": " a[i]; }'
4:
5:
6:
7:
8:
1:
2:
3: