简体   繁体   English

在awk命令中不能使用“(”

[英]can not use “(” in awk command

I tried following awk command in my Linux box, it throws error. 我尝试在Linux框中遵循awk命令,但会引发错误。

awk -F "VALUES(" '{print $2}'

Error : 错误:

awk: fatal: Unmatched ( or \(: /VALUES(/

I also tried with back slash, its also not working. 我还尝试了反斜杠,它也无法正常工作。

awk -F "VALUES\\(" '{print $2}'

Error : 错误:

awk: warning: escape sequence `\(' treated as plain `(' 
awk: fatal: Unmatched ( or \(: /VALUES(/

Please let me know how to include ( in awk search string. 请让我知道如何在awk搜索字符串中包含(。

if the value of -F is longer than 1, it was considered as regex, so you need do something like: 如果-F的值大于1,则将其视为正则表达式,因此您需要执行以下操作:

regex character class: 正则表达式字符类:

kent$  echo "a foo( b"|awk -F"foo[(]" '{print $1,$2}'
a   b

escape the ( 逃脱(

if you really want to escape the ( , you need: 如果您确实想逃脱( ,则需要:

kent$  echo "a foo( b"|awk -F"foo\\\\(" '{print $1,$2}'
a   b

or 要么

kent$  echo "a foo( b"|awk -F'foo\\(' '{print $1,$2}'  
a   b

You're using Linux, so you're likely using gawk . 您使用的是Linux,因此很可能使用的是gawk From the gawk man page on my system: 从我系统上的gawk手册页:

   -F fs
   --field-separator fs
          Use fs for the input field separator (the value of the FS prede-
          fined variable).

and: 和:

Fields 字段

As each input record is read, gawk splits the record into fields, using the value of the FS variable as the field separator. 读取每个输入记录时,gawk会将FS变量的值用作字段分隔符,将记录分成多个字段。 If FS is a single character, fields are separated by that character. 如果FS是单个字符,则字段由该字符分隔。 If FS is the null string, then each individual character becomes a separate field. 如果FS为空字符串,则每个单独的字符将成为一个单独的字段。 Otherwise, FS is expected to be a full regular expression. 否则,FS应该是完整的正则表达式。

Since you've got multiple characters in your -F , it's being interpreted as a regular expression. 由于-F多个字符,因此将其解释为正则表达式。 And you have an opening bracket without a closing bracket. 而且您有一个没有封闭括号的左括号。

You may be able solve this either by escaping the bracket ( "VALUES\\(" ), or by putting the bracket in a group ( "VALUES[(]" ). I recommend the latter, because escaping things with backslashes can be ugly and unpredictable, and will help point out to you that this is a regex, not a string, when you re-read this script a few months from now. 您可以通过转义括号( "VALUES\\(" ),或将方括号放在一个组中( "VALUES[(]" )来解决此问题。我建议使用后者,因为用反斜杠转义的内容可能很难看,并且不可预测,当您从现在开始几个月后重新阅读此脚本时,它将帮助您指出这是一个正则表达式,而不是字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM