linux - 如何将 awk 与多值定界符一起使用

标签 linux shell unix

我如何使用包含多值的 awk 定界符:“#@$”

我有这样的文件:Test1#@$Test2#@$Test3#@$Test4 我需要提取“Test2”。 在我执行此命令后:awk -F "#@$"'{print $2}',没有显示任何内容>

然后 awk -F "#@$"'{print $1}' 我得到整行

有什么想法吗?

最佳答案

您遇到的问题是字段分隔符 FS 被认为是正则表达式。 字符 ($) 在正则表达式中具有特殊含义,因为它代表行尾的 anchor 。解决方案是将它转义两次,因为 -escapes 被解释了两次;一次在字符串的词法处理中,一次在处理正则表达式中:

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 the FS variable shall be a single <space>. The following describes FS behaviour:

  1. If FS is a null string, the behaviour is unspecified.
  2. 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 character c, fields shall be delimited by every single occurrence of c.
  3. 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 EREs ef$ and (ef$) match ef in the string abcdef, but fail to match in the string cdefab, and the ERE e$f is valid, but can never match because the f prevents the expression e$ from matching ending at the last character.

source: POSIX Extended Regular Expressions

关于linux - 如何将 awk 与多值定界符一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53409603/

相关文章:

Bash - 多个替换为 sed 语句

linux - meteor 安装失败 - 仅达到 45%

c++ - Linux 中 C++ Tcp 通信的间歇性延迟

bash - 无法理解 bash 脚本中的组合执行和重定向

linux - 使用文字双引号从 bash 脚本生成文本

linux - 当用户在 linux/unix 中键入 size 命令时,结果是什么意思?

linux - Ash shell - 如何在函数内进行 while 循环?

linux - 查找特定文件夹,然后在其中的特定文件中搜索一个词

android - 无法从 adb shell 运行可执行文件

用于从ubuntu服务器获取centOS服务器日期时间的C程序?