简体   繁体   English

为什么我的awk命令会打印一个额外的行?

[英]Why does my awk command print an extra line?

I'm finding it difficult to debug my shell scripts. 我发现很难调试我的shell脚本。 I have the following file test.csv 我有以下文件test.csv

The Gardens,Gard (AUS),AEST,NSW,,Sandown Park,SPrk (AUS),AEST,VIC,,Grade 5,GR5,
Wentworth Park,WPrk (AUS) ,AEST,NSW,,The Meadows,Mead (AUS),AEST,VIC,,Juvenille,JUV,
Angle Park,AnPk (AUS),ACST,SA,,Warragul,Wgul (AUS),AEST,VIC,,,,

WPrk (AUS) has an extra trailing white space which I wish to trim and then print. WPrk(AUS)有一个额外的尾随空白区域,我希望修剪然后打印。 Running 运行

awk -F\, 'gsub(/[ \t]+$/, "", $2); {print $2 ":"}' test.csv

Produces 产生

Gard (AUS):
Wentworth Park WPrk (AUS) AEST NSW  The Meadows Mead (AUS) AEST VIC  Juvenille JUV
WPrk (AUS):
AnPk (AUS):

Which is what I want except for the second line I can't figure out why that appears 这是我想要的,除了第二行,我无法弄清楚为什么会出现

awk -F\, 'gsub(/[ \t]+$/, "", $2); {print $2 ":"}' test.csv

Since you have gsub outside any action block ( {...} ), it is evaluated every line as a condition . 由于您在任何操作块( {...} )之外都有gsub ,因此将每行作为条件进行评估。 It returns the number of substitutions it made, so it will return 0 on most of the inputs, but 1 on the lines you want to change. 它返回它所做的替换次数,因此它将在大多数输入上返回0,但在要更改的行上返回1。 Since there is no action accompanying it, the default action is executed, printing the line; 由于没有伴随它的动作,执行默认动作,打印线; that is why you are getting the full line printed. 这就是你打印全行的原因。

The fix, as others have said, is putting the gsub call inside the action block. 正如其他人所说的那样,修复程序将gsub调用放在action块中。

(Also, why \\, ? Comma is not a special character in the shell.) (另外,为什么\\, ,?逗号不是shell中的特殊字符。)

How about changing the command to be: 如何将命令更改为:

    awk -F, '{gsub(/[ \t]+$/, "", $2); print $2 ":"}' test.csv

That's moving '{' to the front. 那是'{'向前移动。

Edited As others said, you don't need to escape comma, -F, is enough. 编辑正如其他人所说,你不需要逃脱逗号, -F,就足够了。 Thanks to other answerers :). 感谢其他回答者:)。

You wrote: 你写了:

awk -F\, 'gsub(/[ \t]+$/, "", $2); {print $2 ":"}' test.csv

This is broken. 这已破了。

The format for an awk script is a bunch of constructs that look like: awk脚本的格式是一堆看起来像这样的结构:

condition {
     command; 
     ... 
    }

The idea here is that each line in the input data is evaluated against condition . 这里的想法是输入数据中的每一行都是根据条件进行评估的。 If it matches, then the commands in the corresponding curly brackets are executed. 如果匹配,则执行相应大括号中的命令。 So what you probably want is more along the lines of this: 所以你可能想要的更像是这样:

awk -F, '{gsub(/[ \t]+$/, "", $2); print $2 ":";}' test.csv

Note that you don't need to escape the field separator unless your shell will treat it badly (ie if it was a vertical bar, | , instead of a comma). 请注意,您不需要转义字段分隔符,除非您的shell会严重对待它(即如果它是垂直条, | ,而不是逗号)。 By excluding the condition inside the script, you execute the curly-braced commands on EVERY line. 通过排除脚本中的条件,可以在每条线上执行花括号命令。 So this will trim $2 whether it needs to be trimmed or not, then print the result per your example in your question. 因此,无论是否需要修剪,都会削减$ 2,然后根据您的示例在您的问题中打印结果。

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

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