我如何使用包含多值的 awk
定界符:“#@$”
我有这样的文件:Test1#@$Test2#@$Test3#@$Test4
我需要提取“Test2”。
在我执行此命令后:awk -F "#@$"'{print $2}'
,没有显示任何内容>
然后 awk -F "#@$"'{print $1}'
我得到整行
有什么想法吗?
最佳答案
您遇到的问题是字段分隔符 FS
被认为是正则表达式。 $
) 在正则表达式中具有特殊含义,因为它代表行尾的 anchor 。解决方案是将它转义两次,因为
awk -F '#@\\$' '{print $1}'
An extended regular expression can be used to separate fields by assigning a string containing the expression to the built-in variable
FS
, either directly or as a consequence of using the-F
sepstring option. The default value of theFS
variable shall be a single <space>. The following describesFS
behaviour:
- If
FS
is a null string, the behaviour is unspecified.If
FS
is a single character:
- If
FS
is <space>, skip leading and trailing <blank> and <newline> characters; fields shall be delimited by sets of one or more <blank> or <newline> characters.- Otherwise, if
FS
is any other characterc
, fields shall be delimited by every single occurrence ofc
.Otherwise, the string value of
FS
shall be considered to be an extended regular expression. Each occurrence of a sequence matching the extended regular expression shall delimit fields.source: POSIX awk standard
A <dollar-sign> (
$
) outside a bracket expression shall anchor the expression or subexpression it ends to the end of a string; such an expression or subexpression can match only a sequence ending at the last character of a string. For example, the EREsef$
and(ef$)
matchef
in the stringabcdef
, but fail to match in the stringcdefab
, and the EREe$f
is valid, but can never match because thef
prevents the expressione$
from matching ending at the last character.
关于linux - 如何将 awk 与多值定界符一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53409603/