I am trying to create a csv file with AWK. I am using output file separator to add commas for columns. But there is one column whose name is separated by space and Its also getting processed in the output with comma.
cat test.txt | head -n 3
Vulnerability ID Package Severity Fix Vulnerability URL
CVE-2017-1000408 libc-bin-2.24-11+deb9u3 High 2.24-11+deb9u4 https://security-tracker.debian.org/tracker/CVE-2017-1000408
CVE-2017-1000408 libc-dev-bin-2.24-11+deb9u3 High 2.24-11+deb9u4 https://security-tracker.debian.org/tracker/CVE-2017-1000408
Command:
cat test.txt | awk -F' ' 'BEGIN{OFS=",";} {print $1,$2,$3,$4,$5;}' > file.csv
Output:
Vulnerability,ID,Package,Severity,Fix
CVE-2017-1000408,libc-bin-2.24-11+deb9u3,High,2.24-11+deb9u4,https://security-tracker.debian.org/tracker/CVE-2017-1000408
CVE-2017-1000408,libc-dev-bin-2.24-11+deb9u3,High,2.24-11+deb9u4,https://security-tracker.debian.org/tracker/CVE-2017-1000408
Expected Output:
Vulnerability ID,Package,Severity,Fix,Vulnerability URL
CVE-2017-1000408,libc-bin-2.24-11+deb9u3,High,2.24-11+deb9u4,https://security-tracker.debian.org/tracker/CVE-2017-1000408
CVE-2017-1000408,libc-dev-bin-2.24-11+deb9u3,High,2.24-11+deb9u4,https://security-tracker.debian.org/tracker/CVE-2017-1000408
TAB
you may just use-F "\t"
; for multiple (one or more) tabs,-F "\t+"
; for a single space,-F [ ]
; for two or more spaces,-F [ ]{2,}
. And so on...csvformat
.awk
would not correctly hands fields that include comma.-F '[ ]'
,-F '[ ]{2,}'
).