Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extra space in awk output

Tags:

awk

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
like image 652
shantanuo Avatar asked Mar 01 '12 06:03

shantanuo


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 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
like image 150
potong Avatar answered Sep 28 '22 08:09

potong


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.

like image 29
jfg956 Avatar answered Sep 28 '22 07:09

jfg956