Extra space in awk output

眉间皱痕 提交于 2021-01-27 06:26:18

问题


Why do I get the white space before and after the delimiter in the following example?

awk -F'^'  '{print $1,":",$2}' SERVER_2012-02-29-12-15-00
3969 : 1272
3969 : 1272
3969 : 1272

I expect the results as below without any space:

3969:1272
3969:1272
3969:1272

The text file looks like this...

cat SERVER_2012-02-29-12-15-00
3969^1272^14.140.90.242^^IN^como^2012-02-29
3969^1272^14.140.90.242^^IN^como^2012-02-29
3969^1272^14.140.90.242^^IN^como^2012-02-29

回答1:


This might work for you:

awk -F'^'  '{print $1":"$2}' SERVER_2012-02-29-12-15-00
3969:1272
3969:1272
3969:1272

To concatenate the fields with a : remove the ,'s.

Or change the output field separator OFS to null.

awk -F'^' -vOFS='' '{print $1",:,"$2}' SERVER_2012-02-29-12-15-00
3969:1272
3969:1272
3969:1272



回答2:


Solutions have been given, but no complete explanation...

It is because when you use print with many parameters (separated by coma) it puts a Field Separator between the values. From the awk man:

print expr-list

Print expressions. Each expression is separated by the value of the OFS variable. The output record is terminated with the value of the ORS variable.

The string concatenation operator in awk is not coma, neither +, but space: str = str1 str2 concatenate str1 and str2.



来源:https://stackoverflow.com/questions/9511803/extra-space-in-awk-output

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!