如何将重复字符转换为文本文件中的新行

时间:2017-06-22 18:11:51

标签: shell unix sed format tr

我有一个类似于此的文本文件(不幸的是,这是数据的提取方式):

asdf,10.vip0,sample_pool1,;;;;;;;;10.server1;;;;;;;;;;11.server2;;;;;;;;;;12.server3;;
asdf,10.vip1,sample_pool2,;;;;;;;;10.server1;;;;;;;;;;11.server2;;;;;;;;;;12.server3;;
asdf,10.vip2,sample_pool3,;;;;;;;;10.server1;;;;;;;;;;11.server2;;;;;;;;;;12.server4;;

我可以使用命令将其格式化为这样吗?

asdf,10.vip0,sample_pool1,10.server1
11.server2
12.server3
asdf,10.vip1,sample_pool2,10.server41
11.server42
12.server43
asdf,10.vip2,sample_pool3,10.server31
11.server32
12.server34

2 个答案:

答案 0 :(得分:1)

也许是这样的?

$ sed 's/;;;\+//; s/;;;\+/\n/g; s/;;$//' file
asdf,10.vip0,sample_pool1,10.server1
11.server2
12.server3
asdf,10.vip1,sample_pool2,10.server1
11.server2
12.server3
asdf,10.vip2,sample_pool3,10.server1
11.server2
12.server4

它删除第一组重复分号s/;;;\+//然后用新行s/;;;\+/\n/g替换所有剩余的组,最后在末尾删除两个分号s/;;$//

答案 1 :(得分:1)

awk 方法:

awk -F, '{ sub(/,;+/,",",$0); sub(/;+$/,"",$0); gsub(/;{3,}/,"\n",$0) }1' file

输出:

asdf,10.vip0,sample_pool1,10.server1
11.server2
12.server3
asdf,10.vip1,sample_pool2,10.server1
11.server2
12.server3
asdf,10.vip2,sample_pool3,10.server1
11.server2
12.server4