我正在Linux中编写脚本,脚本的结果是一个csv文件。我想格式化该文件。我的输出文件是:
"T_Hours","T_Count","T_Hours","P_Avg_5","Diffrence_Between_P_Avg_5_and_T_Count"
"00","446","00","533","87"
"01","159","01","224","65"
"02","95","02","140","45"
"03","84","03","117","33"
"04","92","04","113","21"
我想这样输出:
"T_Hours","T_Count","T_Hours","P_Avg_5","Diffrence_Between_P_Avg_5_and_T_Count"
"00" ,"446" ,"00" ,"533" ,"87"
"01" ,"159" ,"01" ,"224" ,"65"
"02" ,"95" ,"02" ,"140" ,"45"
"03" ,"84" ,"03" ,"117" ,"33"
"04" ,"92" ,"04" ,"113" ,"21"
我尝试的方法:我尝试在,之后和之前添加空格,但这不起作用,因为数字并不总是具有相同的宽度。例如,第二列中的数字可能为"55556"
,因此添加特定数量的空格将不起作用。因此,我认为解决方案是将字符,
移至所有行,并将其放在第一行的,
下。有什么帮助吗?
答案 0 :(得分:4)
这是一种可移植的方式:
sed 's/,/:,/g' output.csv |
column -t -s: |
sed 's/ ,/,/g'
说明:
column -t
对齐列。可以使用-s
指定输入分隔符,但是输出分隔符始终是一个空格(除非您有支持{{1的 GNU 或 util-linux 之类的版本}}标志,请参见this answer。
使用-o
,输出将只是
column -s, -t output.csv
要在输出中保留"T_Hours" "T_Count" "T_Hours" ...
"00" "446" "00" ...
"01" "159" "01" ...
...
,我们必须进行预处理和后处理:
,
在每个逗号分隔符的前面插入一个附加的分隔符sed
。您选择的字符必须是输入中没有的字符。:
指定该column -t
字符作为分隔符。 :
将对齐所有列,并用空格替换所有column
。:
删除空格(最初是我们在第一个命令中插入的sed
)。:
更多信息,请参见"T_Hours","T_Count","T_Hours",...
"00" ,"446" ,"00" ,...
"01" ,"159" ,"01" ,...
...
。
答案 1 :(得分:2)
$ column -t -s, -o, file
"T_Hours","T_Count","T_Hours","P_Avg_5","Diffrence_Between_P_Avg_5_and_T_Count"
"00" ,"446" ,"00" ,"533" ,"87"
"01" ,"159" ,"01" ,"224" ,"65"
"02" ,"95" ,"02" ,"140" ,"45"
"03" ,"84" ,"03" ,"117" ,"33"
"04" ,"92" ,"04" ,"113" ,"21"
答案 2 :(得分:1)
这可能对您有用(GNU sed):
sed -r '1{p;s/[^,]/ /g;h;d};G;s/^/\n/;:a;ta;s/\n([^,])(.*\n)\s/\1\n\2/;ta;s/\n,(.*\n)([^,]*,)/\2\n\1/;ta;P;d' file
这使用第一行的标题设置列宽。每个数据列都写在一个由标题创建的空格分隔的模板上。