49

When I run the following

awk -F\, '{print $2,":",$1}'

It prints

"First : Second"

How can I get

"First:Second"
Hoa
  • 19,858
  • 28
  • 78
  • 107

3 Answers3

80

Omit the ,s

awk -F\, '{print $2 ":" $1}'
Alex Lockwood
  • 83,063
  • 39
  • 206
  • 250
je4d
  • 7,628
  • 32
  • 46
  • 11
    this works because using the commas inserts OFS between the print arguments. You could also write `awk -F, -v OFS=: '{print $2, $1}'` – glenn jackman Apr 03 '12 at 00:00
  • in some cases the ',' are needed with awk's arithmetic to yield correct OFMT value, or i am doing something wrong – StevenWernerCS Nov 07 '20 at 04:14
8

Try this:

awk -F\, '{print $2":"$1}'
ldav1s
  • 15,885
  • 2
  • 53
  • 56
2
 awk '{printf "%s:%s",$2,$1}'

You can also use printf instead of print to get more control over printing things.

Ruman
  • 936
  • 11
  • 9