我有一个文件,PoolReport.txt
.这是一些示例数据:
Thu, Aug 27, 2015 7:56:29 AM
PoolName,Total GB,Used GB ,Available GB ,Percent Full
Oracle-RAID10,6432.539,6179.295,253.244,96.063
VM_File,45210.379,40726.389,4483.991,90.082
Oracle-RAID5,15025.039,12289.707,2735.332,81.795
我需要它采用以下格式。
Thu, Aug 27, 2015 7:56:29 AM
PoolName Total GB Used GB Available GB Percent Full
Oracle-RAID10 6432.539 6179.295 253.244 96.063
VM_File 45210.379 40726.389 4483.991 90.082
Oracle-RAID5 15025.039 12289.707 2735.332 81.795
我设法制作了一个更好的格式(如下),但它不能满足需要。
awk -F "," '{print $1"\t" $2"\t" $3"\t" $4"\t" $5"\t" }' PoolReport.txt
Thu Aug 27 2015 7:56:29 AM
PoolName Total GB Used GB Available GB Percent Full
Oracle-RAID10 6432.539 6179.295 253.244 96.063
VM_File 45210.379 40726.389 4483.991 90.082
Oracle-RAID5 15025.039 12289.707 2735.332 81.795
答案1
我不确定我是否正确理解你的问题,但我相信你想对齐列。如果是这种情况,那么命令column
就很方便:
column -s, -t file
输出:
Thu Aug 27 2015 7:56:29 AM
PoolName Total GB Used GB Available GB Percent Full
Oracle-RAID10 6432.539 6179.295 253.244 96.063
VM_File 45210.379 40726.389 4483.991 90.082
Oracle-RAID5 15025.039 12289.707 2735.332 81.795
或者,如果您想保持第一行不变,那么
{ head -n 1; column -s, -t; } <file
输出:
Thu, Aug 27, 2015 7:56:29 AM
PoolName Total GB Used GB Available GB Percent Full
Oracle-RAID10 6432.539 6179.295 253.244 96.063
VM_File 45210.379 40726.389 4483.991 90.082
Oracle-RAID5 15025.039 12289.707 2735.332 81.795
请参阅man column
您可能想要添加以调整输出的可能选项。
答案2
更改所有出现的,
to {tab}
,第一行除外:
sed '2,$s/ *, */\t/g' PoolReport.txt
或者使用制表符分隔符格式化字段,第一行除外:
awk -F, 'NR==1;NR>1{OFS="\t"; print $1,$2,$3,$4,$5}' PoolReport.txt
任一实例的输出:
Thu, Aug 27, 2015 7:56:29 AM
PoolName Total GB Used GB Available GB Percent Full
Oracle-RAID10 6432.539 6179.295 253.244 96.063
VM_File 45210.379 40726.389 4483.991 90.082
Oracle-RAID5 15025.039 12289.707 2735.332 81.795