Reputation: 55
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
Reputation: 4865
awk '{print $13 ";" $10}' >> CPU2.csv
awk '{print $13, $10}' OFS=";" >> CPU2.csv
awk '{printf("%s;%s\n", $13, $10)}' >> CPU2.csv
Upvotes: 2
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