我的文件中有以下数据:
col1 col2 col3 subcol1 subcol2 subcol3
subcol4 subcol5 subcol6
col4 subcol7 subcol8 subcol9
subcol10 subcol11 subcol2
数据由空格分隔。没有选项卡我想转换此数据,如下所示:
-------------------------------------------------------
col1 | col2 | col3 | subcol1 | subcol2 | subcol13
--------------------------------
| | | subcol4 | subcol5 | subcol6
-----------------------------------------
| | col4 | subcol7 | subcol8 | subcol9
--------------------------------
| | | subcol10 | subcol11| subcol12
-------------------------------------------------------
我怎样才能做到这一点?
已编辑
我已经尝试过以下命令
sed -r -e 's/ {5}/\t/g' -e 's/ /\t/g' file.in | sed -e 's/\t/_|/g'| column -t -s '_'
它产生
col1 |col2 |col3 |subcol1 |subcol2 |subcol13
| | |subcol4 |subcol5 |subcol6
| |col4 |subcol7 |subcol8 |subcol9
| | |subcol10 |subcol11 |subcol12
它只需要那条线来填充这个
答案1
我最终得到了以下awk
脚本。
awk '{gsub(/ {1,5}/,"\t|")}1' infile.txt | \
awk -F'|' 'NR>1{for(i=1;i<=NF;i++) if ($i=="\t") c++;
printf("%*s",(c*8),"\t-");
for (fill=1;fill<=length($0)+c;fill++)printf "-";
printf("\n%s\n",$0);c=0;next} {print $0}'
输出是:
col1 |col2 |col3 |subcol1 |subcol2 |subcol3
-----------------------------------
| | |subcol4 |subcol5 |subcol6
--------------------------------------
| |col4 |subcol7 |subcol8 |subcol9
-------------------------------------
| | |subcol10 |subcol11 |subcol2