gpt4 book ai didi

linux - 将分隔字段移动到具有不同分隔符的新行中[再次更新]

转载 作者:太空宇宙 更新时间:2023-11-04 09:39:26 28 4
gpt4 key购买 nike

我有一个逗号分隔的 txt 文件,它必须只有五列,但有些行有超过 5 列。
我想将第 6 到第 10 移到换行符,第 11 到第 15 移到换行符,依此类推。并且第 6、11、16 等列有空格 deleimert 而不是逗号

下面是input.txt的内容

111 1, 2, 3, 4, 5
11 2, 13, 14, 15 5, 16 11, 17, 18, 19, 20
22, 23, 24, 25, 26 22, 27, 28, 29, 21 30, 31, 32, 3333 3, 34

下面是Output.txt的内容

111 1, 2, 3, 4, 5
11 2, 13, 14, 15 5, 16
11, 17, 18, 19, 20
22, 23, 24, 25, 26
22, 27, 28, 29, 21
30, 31, 32, 3333 3, 34

最佳答案

尝试:

$ cat f1
1,2,3,4,5
12,13,14,15,16 11,17,18,19,20
22,23,24,25,26 22,27,28,29,21 30,31,32,33,34

$ awk '1' RS=' |\n' f1
1,2,3,4,5
12,13,14,15,16
11,17,18,19,20
22,23,24,25,26
22,27,28,29,21
30,31,32,33,34

用户更新输入以上解决方案将不起作用

$ cat f2
1, 2, 3, 4, 5
12, 13, 14, 15, 16 11, 17, 18, 19, 20
22, 23, 24, 25, 26 22, 27, 28, 29, 21 30, 31, 32, 33, 34

$ awk '{gsub(/, /,",");gsub(/ /,"\n");gsub(/,/,", ")}1' f2

OR

$ awk '{gsub(/[[:alnum:]] /,"&\n")}1' f2

1, 2, 3, 4, 5
12, 13, 14, 15, 16
11, 17, 18, 19, 20
22, 23, 24, 25, 26
22, 27, 28, 29, 21
30, 31, 32, 33, 34

回复以下评论

gsub(/, /,",")     # Substitute comma for comma + space

gsub(/ /,"\n") # So now (field + space + field) is left, substitute space with newline

gsub(/,/,", ") # substitute comma space (as you requested in expected output) for comma (first argument)

关于linux - 将分隔字段移动到具有不同分隔符的新行中[再次更新],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22979955/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com