Carolyn
Carolyn

Reputation: 55

How to put a comma in between awk when filtering columns in bash shell script?

I want put a comma in between outputs from awk in bash shell script (linux).

This is a snippet of my original command

awk {print $13, $10} >> test.csv

If I put a comma in between $13 and $10, I would get a space in between the two columns But what I want is a comma between these two columns

I'm very new to this and I can't find any resources about this online so bear with me if this is a simple mistake. Thank you

Upvotes: 0

Views: 1273

Answers (3)

Dudi Boy
Dudi Boy

Reputation: 4865

suggestion 1

  awk '{print $13 ";" $10}' >> CPU2.csv

suggestion 2

  awk '{print $13, $10}' OFS=";" >> CPU2.csv

suggestion 3

 awk '{printf("%s;%s\n", $13, $10)}' >> CPU2.csv

Upvotes: 2

RARE Kpop Manifesto
RARE Kpop Manifesto

Reputation: 2815

not as elegant as I hoped, but this should work :

mawk 'NF=($+_=$13(_)$10)~_' \_=\;

It first overwrites the entire row with just $13 and $10, with the semi-colon ; in between. _ is a semi-colon, thus numerical evaluation of $+_ is identical to $0, and since I've forced the delimiter in between them, the regex test of presence of semi-colon will always yield true (1), making NF = 1, and printing just that.

Assigning 1 into NF in lieu of $1 = $1.

NF isn't a 2 because I'm using the new sep in between them, instead of space or tab, so even though $0 was overwritten, awk wouldn't have found the sep it needed to split out 2nd field.

Tested on mawk 1.3.4, mawk 1.9.9.6, macOS nawk, and gawk 5.1.1, including gawk -t traditional flag and gawk -P posix mode.

-- The 4Chan Teller

Upvotes: 0

mrqiao001
mrqiao001

Reputation: 152

echo "1 2 3"|awk '{a=";";print $1a$2","$3}'

Upvotes: 0

Related Questions