Extra space in awk output

Why am I getting a space before and after the separator in the following example?

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

I expect results as shown below without a space:

 3969:1272 3969:1272 3969:1272 

The text file is as follows:

 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 
+4
source share
2 answers

This might work for you:

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

To combine the fields with : delete,.

Or change the OFS output field delimiter to null.

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

Decisions were given, but no full explanation ...

This is due to the fact that when using print with many parameters (separated by a coma), it places a field separator between the values. From awk man:

print expr-list

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

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

+5
source

Source: https://habr.com/ru/post/1399130/


All Articles