如何漂亮地打印逗号分隔文件?

时间:2017-05-16 04:52:05

标签: bash awk sed pretty-print

以下是我的档案。我试图在输出之间保持恒定的宽度,以便所有管道符号都低于另一个,它看起来像表格式。我怎样才能做到这一点?我邮寄输出时也需要格式相同。

cat file.txt
name,score card,ID
Jack,Available,YES
Jen,NA,NO
Jerry,Available,YES
Padula,NA,YES
Mackenzi,NA,NO

sed 's/,/\t|\t/g' c.txt
name    |       score card      |       ID
Jack    |       Available       |       YES
Jen     |       NA      |       NO
Jerry   |       Available       |       YES
Padula  |       NA      |       YES
Mackenzi        |       NA      |       NO

==I want==
name      |       score card      |       ID
Jack      |       Available       |       YES
Jen       |       NA              |       NO
Jerry     |       Available       |       YES
Padula    |       NA              |       YES
Mackenzi  |       NA              |       NO

2 个答案:

答案 0 :(得分:5)

$ sed 's/,/,|,/g' file | column -t -s,
name      |  score card  |  ID
Jack      |  Available   |  YES
Jen       |  NA          |  NO
Jerry     |  Available   |  YES
Padula    |  NA          |  YES
Mackenzi  |  NA          |  NO

答案 1 :(得分:0)

如果您希望使用awk存档,

同样如此
awk -F"," '{printf "%-10s%s%-10s%s%-10s\n", $1,OFS,$2,OFS,$3}' OFS="|" file.txt