我正在尝试使用 AWK 创建 csv 文件。我正在使用输出文件分隔符为列添加逗号。但有一列的名称由空格分隔,并且它也在输出中用逗号进行处理。
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
命令:
cat test.txt | awk -F' ' 'BEGIN{OFS=",";} {print $1,$2,$3,$4,$5;}' > file.csv
输出:
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
预期输出:
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
答案1
我的建议是以不同于文件其余部分的方式详细说明标题行。在这个特殊情况下:
awk 'NR==1 {$0=gensub(/(Vulnerability) (ID|URL)/, "\\1_\\2", "g"); print gensub(/(Vulnerability)_(ID|URL)/, "\\1 \\2", "g", $1","$2","$3","$4","$5)} NR>1 {print $1","$2","$3","$4","$5}' test.txt > file.csv
或者:
sed -r '1s/(Vulnerability) (ID|URL)/\1_\2/g' test.txt | awk '{print $1","$2","$3","$4","$5}' | sed -r '1s/(Vulnerability)_(ID|URL)/\1 \2/g' > file.csv
您在评论中收到了一些非常合适的建议,特别是关于分割多个空格字符的建议以及建议您使用其他工具来操作 CSV 文件的建议。此外,如果您的问题仅在于标题行,如果我是您,我会手动修复它。还有……你真的吗需要标题中是否有空格(而不是Vulnerability_URL
)?这一切都取决于您的实际用例。
对我来说,分割多个空间似乎是最简单的:
sed 's/ */,/g' test.txt > file.csv