您好我有以下文字,我需要转换为适当的制表符分隔列。还保留了与列对应的标题的美学含义。
Ximt@@@Xypt@@@Rqistr-wy@@@Nort@@@LU-nqmt@@@Minor@@@Kqust@@@Rtqson
------------------------------------------------------------------------------------------------------------------------
2017-03-20X05:17:09.595-0500@@@Olqrm@@@VUKLRE-LK-1@@@VUKLRE-KP-10@@@MLU-2@@@5636100@@@418@@@MLU Oppl LLL
2017-03-20X05:17:11.929-0500@@@K-log@@@VUKLRE-LK-1@@@VUKLRE-KP-10@@@N/O@@@5636122@@@418@@@MLU stqtt ehqngt notiyieqtion
2017-03-20X05:17:12.250-0500@@@K-log@@@VUKLRE-LK-1@@@VUKLRE-KP-10@@@N/O@@@5636122@@@418@@@MLU stqtt ehqngt notiyieqtion
2017-03-20X05:17:12.702-0500@@@Olqrm@@@VUKLRE-LK-1@@@VUKLRE-KP-10@@@MLU-2@@@5636100@@@418@@@MLU Oppl LLL
2017-03-20X05:17:12.990-0500@@@Olqrm@@@VUKLRE-LK-1@@@VUKLRE-KP-10@@@MLU-2@@@5636100@@@418@@@MLU Oppl LLL
我试过了,
awk -v FS='@@@' -v OFS='\t\t' '{$1=$1}1' input
和
column -t -s'@@@' <input
结果搞砸了,如何用相应的列制作标题?像下面的东西?
Ximt Xypt Rqistr-wy Nort LU-nqmt Minor Kqust Rtqson
------------------------------------------------------------------------------------------------------------------------
2017-03-20X05:17:09.595-0500 Olqrm VUKLRE-LK-1 VUKLRE-KP-10 MLU-25636100 418 MLU Oppl LLL
答案 0 :(得分:2)
你能活下去吗?
$ sed '/---/d' file | column -t -s'@@@'
Ximt Xypt Rqistr-wy Nort LU-nqmt Minor Kqust Rtqson
2017-03-20X05:17:09.595-0500 Olqrm VUKLRE-LK-1 VUKLRE-KP-10 MLU-2 5636100 418 MLU Oppl LLL
2017-03-20X05:17:11.929-0500 K-log VUKLRE-LK-1 VUKLRE-KP-10 N/O 5636122 418 MLU stqtt ehqngt notiyieqtion
2017-03-20X05:17:12.250-0500 K-log VUKLRE-LK-1 VUKLRE-KP-10 N/O 5636122 418 MLU stqtt ehqngt notiyieqtion
2017-03-20X05:17:12.702-0500 Olqrm VUKLRE-LK-1 VUKLRE-KP-10 MLU-2 5636100 418 MLU Oppl LLL
2017-03-20X05:17:12.990-0500 Olqrm VUKLRE-LK-1 VUKLRE-KP-10 MLU-2 5636100 418 MLU Oppl LLL
在标题下添加一行破折号可以是以下任何一种:
$ sed '/---/d' file | column -t -s'@@@' | awk '1; NR==1{gsub(/[^ ]/,"-"); print}'
Ximt Xypt Rqistr-wy Nort LU-nqmt Minor Kqust Rtqson
---- ---- --------- ---- ------- ----- ----- ------
2017-03-20X05:17:09.595-0500 Olqrm VUKLRE-LK-1 VUKLRE-KP-10 MLU-2 5636100 418 MLU Oppl LLL
2017-03-20X05:17:11.929-0500 K-log VUKLRE-LK-1 VUKLRE-KP-10 N/O 5636122 418 MLU stqtt ehqngt notiyieqtion
2017-03-20X05:17:12.250-0500 K-log VUKLRE-LK-1 VUKLRE-KP-10 N/O 5636122 418 MLU stqtt ehqngt notiyieqtion
2017-03-20X05:17:12.702-0500 Olqrm VUKLRE-LK-1 VUKLRE-KP-10 MLU-2 5636100 418 MLU Oppl LLL
2017-03-20X05:17:12.990-0500 Olqrm VUKLRE-LK-1 VUKLRE-KP-10 MLU-2 5636100 418 MLU Oppl LLL
$ sed '/---/d' file | column -t -s'@@@' | awk 'NR==2{x=$0; gsub(/[^ ]/,"-",x); print x} 1'
Ximt Xypt Rqistr-wy Nort LU-nqmt Minor Kqust Rtqson
---------------------------- ----- ----------- ------------ ----- ------- --- --- ---- ---
2017-03-20X05:17:09.595-0500 Olqrm VUKLRE-LK-1 VUKLRE-KP-10 MLU-2 5636100 418 MLU Oppl LLL
2017-03-20X05:17:11.929-0500 K-log VUKLRE-LK-1 VUKLRE-KP-10 N/O 5636122 418 MLU stqtt ehqngt notiyieqtion
2017-03-20X05:17:12.250-0500 K-log VUKLRE-LK-1 VUKLRE-KP-10 N/O 5636122 418 MLU stqtt ehqngt notiyieqtion
2017-03-20X05:17:12.702-0500 Olqrm VUKLRE-LK-1 VUKLRE-KP-10 MLU-2 5636100 418 MLU Oppl LLL
2017-03-20X05:17:12.990-0500 Olqrm VUKLRE-LK-1 VUKLRE-KP-10 MLU-2 5636100 418 MLU Oppl LLL
$ sed '/---/d' file | column -t -s'@@@' | awk '1; NR==1{gsub(/./,"-"); print}'
Ximt Xypt Rqistr-wy Nort LU-nqmt Minor Kqust Rtqson
---------------------------------------------------------------------------------------------------------------------------
2017-03-20X05:17:09.595-0500 Olqrm VUKLRE-LK-1 VUKLRE-KP-10 MLU-2 5636100 418 MLU Oppl LLL
2017-03-20X05:17:11.929-0500 K-log VUKLRE-LK-1 VUKLRE-KP-10 N/O 5636122 418 MLU stqtt ehqngt notiyieqtion
2017-03-20X05:17:12.250-0500 K-log VUKLRE-LK-1 VUKLRE-KP-10 N/O 5636122 418 MLU stqtt ehqngt notiyieqtion
2017-03-20X05:17:12.702-0500 Olqrm VUKLRE-LK-1 VUKLRE-KP-10 MLU-2 5636100 418 MLU Oppl LLL
2017-03-20X05:17:12.990-0500 Olqrm VUKLRE-LK-1 VUKLRE-KP-10 MLU-2 5636100 418 MLU Oppl LLL
$ sed '/---/d' file | column -t -s'@@@' | awk 'NR==2{x=$0; gsub(/./,"-",x); print x} 1'
Ximt Xypt Rqistr-wy Nort LU-nqmt Minor Kqust Rtqson
---------------------------------------------------------------------------------------------------------------------------------
2017-03-20X05:17:09.595-0500 Olqrm VUKLRE-LK-1 VUKLRE-KP-10 MLU-2 5636100 418 MLU Oppl LLL
2017-03-20X05:17:11.929-0500 K-log VUKLRE-LK-1 VUKLRE-KP-10 N/O 5636122 418 MLU stqtt ehqngt notiyieqtion
2017-03-20X05:17:12.250-0500 K-log VUKLRE-LK-1 VUKLRE-KP-10 N/O 5636122 418 MLU stqtt ehqngt notiyieqtion
2017-03-20X05:17:12.702-0500 Olqrm VUKLRE-LK-1 VUKLRE-KP-10 MLU-2 5636100 418 MLU Oppl LLL
2017-03-20X05:17:12.990-0500 Olqrm VUKLRE-LK-1 VUKLRE-KP-10 MLU-2 5636100 418 MLU Oppl LLL
答案 1 :(得分:1)
也是perl:
sed '/---/d' co2 | column -t -s'@@@' |\
perl -lnE 'push @l,$_;$l=length $_;$m=$l if $m<$l}{say shift@l;say"-"x$m;say for@l '
输出
Ximt Xypt Rqistr-wy Nort LU-nqmt Minor Kqust Rtqson
----------------------------------------------------------------------------------------------------------------------
2017-03-20X05:17:09.595-0500 Olqrm VUKLRE-LK-1 VUKLRE-KP-10 MLU-2 5636100 418 MLU Oppl LLL
2017-03-20X05:17:11.929-0500 K-log VUKLRE-LK-1 VUKLRE-KP-10 N/O 5636122 418 MLU stqtt ehqngt notiyieqtion
2017-03-20X05:17:12.250-0500 K-log VUKLRE-LK-1 VUKLRE-KP-10 N/O 5636122 418 MLU stqtt ehqngt notiyieqtion
2017-03-20X05:17:12.702-0500 Olqrm VUKLRE-LK-1 VUKLRE-KP-10 MLU-2 5636100 418 MLU Oppl LLL
2017-03-20X05:17:12.990-0500 Olqrm VUKLRE-LK-1 VUKLRE-KP-10 MLU-2 5636100 418 MLU Oppl LLL
它根据输出中的最长行打印分隔线,代价是将所有行读入内存。
可读版本:
sed '/---/d' co2 | column -t -s'@@@' | perl -lnE '
push @l, $_;
$l = length $_;
$m = $l if $m < $l;
END {
say shift @l;
say "-" x $m;
say for @l;
}
'
补充:至少在我column
的{{1}}版本中,-s
并不接受string
作为列分隔符。 -s sep
表示: sep
中的每个(一个)字符将充当分隔符。
e.g。有文件:
foo@@@bar@@@baz@@@qoo
abc@def@@@zyx@zzz
lol=boo@@@fun-goo
column -t -s'@@@'
生成
foo bar baz qoo
abc def zyx zzz
lol=boo fun-goo
所以,你可以清楚地看到它也在单独的@
上对齐。此外,column -t -s'@' code
(一个@
)会产生相同的结果。
foo bar baz qoo
abc def zyx zzz
lol=boo fun-goo
最后使用column -t -s'@='
你会得到
foo bar baz qoo
abc def zyx zzz
lol boo fun-goo
e.g。现在接受@
和 =
作为分隔符。
以上所有意味着,您不需要使用@@@
作为分隔符。 (适用于OS X中的column
的BSD版本):)
Ofc:接受的答案应该是Ed
的答案,这只是为了澄清。
答案 2 :(得分:1)
不是oneliner但是在1 awk(自评论)中完成工作
body
注意: