53

When I run the following

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

It prints

"First : Second"

How can I get

"First:Second"

3 Answers 3

85

Omit the ,s

awk -F\, '{print $2 ":" $1}'
2
  • 11
    this works because using the commas inserts OFS between the print arguments. You could also write awk -F, -v OFS=: '{print $2, $1}' Apr 3, 2012 at 0:00
  • in some cases the ',' are needed with awk's arithmetic to yield correct OFMT value, or i am doing something wrong Nov 7, 2020 at 4:14
8

Try this:

awk -F\, '{print $2":"$1}'
3
 awk '{printf "%s:%s",$2,$1}'

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.