领域我有以下格式的文本文件:由空格,引号或括号定义中的gawk
RANDOM-WORD1 ==> "string with whitespaces" (string with whitespaces)
RANDOM-WORD2 ==> "another string" (and another)
RANDOM-WORD3 ==> "yet another string" (and another)
我想定义gawk
定界符:
- 空格
- 报价
- 括号
例如,第1行:
$1: RANDOM-WORD1
$2: ==>
$3: "string with whitespaces"
$4: (string with whitespaces)
我已阅读gawk
的FPAT
manual和我写了这个:
FPAT = "([^[:blank:]]*)|(\"[^\"]+\")|(\([^)]+\))"
但是,它不会为括号工作,因为我得到:
$1: RANDOM-WORD1
$2: ==>
$3: "string with whitespaces"
$4: (string
我试过在第三个子句中转义括号,但它也不起作用。我想忽略任何不在内的字符(...)
。我知道一个事实,不会有任何嵌套的括号。
注意:我怎样才能忽略引号/圆括号作为字段数据?例如:
$1: RANDOM-WORD1
$2: ==>
$3: string with whitespaces
$4: string with whitespaces
谢谢,这很有效,我也发现了它背后的原因:https://stackoverflow.com/questions/11383643/groovy-why-do-i-need-to-double-escape-square-brackets;它是一个类似的情况? – Adama
@Adama见http://stackoverflow.com/a/36806066/1745001为什么你需要将它们转义两次。btw你可以用'\\ S'替换'[^ [:blank:]]',因为你仍然使用gawk。 –
@Adama在我的理解中,'awk'在调用正则表达式处理引擎之前解释字符串。现在根据[规范](http://pubs.opengroup.org/onlinepubs/009695399/utilities/awk.html#tag_04_06_13_04),'\('不是规范表中列出的有效转义序列,这也是原因为什么gawk显示错误“警告:转义序列”\)'视为普通')'“。 '\('从来没有让它成为正则表达式引擎。为了实现这一点,你需要将反斜杠转义为\\,以便它能够传递字符串处理,而正则表达式引擎可以看到'\('。 – Guido