'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
Solution 1:[1]
suggestion 1
awk '{print $13 ";" $10}' >> CPU2.csv
suggestion 2
awk '{print $13, $10}' OFS=";" >> CPU2.csv
suggestion 3
awk '{prtinf("%s;%s\n", $13, $10)}' >> CPU2.csv
Solution 2:[2]
echo "1 2 3"|awk '{a=";";print $1a$2","$3}'
Solution 3:[3]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Dudi Boy |
| Solution 2 | mrqiao001 |
| Solution 3 | RARE Kpop Manifesto |
