Unix - 第 9 个逗号后分割线

发布于 2024-11-24 02:47:23 字数 318 浏览 1 评论 0原文

我编写了一个 ksh shell 脚本,并且有很长的逗号分隔字符串,仅在第 9 个逗号之后才需要将其分成单独的行。在第 9 个逗号之后,我想删除该逗号并创建一个新行:

例如: 初始字符串 1,2,3,4,5,6,7,8,9,10,11,12,13,14,14,15,16,17,18,19,20,21

输出:

1,2,3,4,5,6,7,8,9,10
11,12,13,14,14,15,16,17,18,19,20
21

我知道这是可能的使用 awk 但我对命令不太熟悉。有人可以提供如何做到这一点

谢谢

I writing a ksh shell script and I have long comma separated string that I need to divide into separate lines only after the 9th comma. After the 9th comma, I want to remove that comma and make a new line:

For Example:
Initial String
1,2,3,4,5,6,7,8,9,10,11,12,13,14,14,15,16,17,18,19,20,21

Output:

1,2,3,4,5,6,7,8,9,10
11,12,13,14,14,15,16,17,18,19,20
21

I know this is possible with awk but I am not so familiar with command. Can someone please provide how to do this

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

勿忘初心 2024-12-01 02:47:35
$ s='1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21'

$ print "$s" | tr , '\n' | paste -d , - - - - - - - - - - | sed -e 's/,\+$//'
1,2,3,4,5,6,7,8,9,10
11,12,13,14,15,16,17,18,19,20
21

$ print "$s" | tr , '\n' | xargs -n 10 echo | tr " " ,
1,2,3,4,5,6,7,8,9,10
11,12,13,14,15,16,17,18,19,20
21
$ s='1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21'

$ print "$s" | tr , '\n' | paste -d , - - - - - - - - - - | sed -e 's/,\+$//'
1,2,3,4,5,6,7,8,9,10
11,12,13,14,15,16,17,18,19,20
21

$ print "$s" | tr , '\n' | xargs -n 10 echo | tr " " ,
1,2,3,4,5,6,7,8,9,10
11,12,13,14,15,16,17,18,19,20
21
白鸥掠海 2024-12-01 02:47:34
cat t.txt | xargs -d, -rn10 | sed 's/ /,/g'

注意:为清楚起见,无用使用cat:这可以是任何进程

根据您的实际需要,删除sed步骤并获取输出空间分隔

奖励点:

输入 (t.txt)

1,2,3,4,5,6,7,8,9,10,11,12,13,14,14,15,16,17,18,19,20,21
22,23
24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50

输出

1,2,3,4,5,6,7,8,9,10
11,12,13,14,14,15,16,17,18,19
20,21
22,23
24,25,26,27,28,29,30,31
32,33,34,35,36,37,38,39,40,41
42,43,44,45,46,47,48,49,50

如果您想要均匀的行填充,请添加粘贴:

粘贴 -sd, t.txt| xargs -d,-n10 | sed 's/ /,/g'

1,2,3,4,5,6,7,8,9,10
11,12,13,14,14,15,16,17,18,19
20,21,22,23,24,25,26,27,28,29
30,31,32,33,34,35,36,37,38,39
40,41,42,43,44,45,46,47,48,49
50
cat t.txt | xargs -d, -rn10 | sed 's/ /,/g'

Note: useless use of cat for clarity: this could be any process

Depending on your actual need, drop the sed step and get the output space delimited

Bonus points:

Input (t.txt)

1,2,3,4,5,6,7,8,9,10,11,12,13,14,14,15,16,17,18,19,20,21
22,23
24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50

Output

1,2,3,4,5,6,7,8,9,10
11,12,13,14,14,15,16,17,18,19
20,21
22,23
24,25,26,27,28,29,30,31
32,33,34,35,36,37,38,39,40,41
42,43,44,45,46,47,48,49,50

If you want homogeneous line-filling, add paste:

paste -sd, t.txt| xargs -d, -n10 | sed 's/ /,/g'

1,2,3,4,5,6,7,8,9,10
11,12,13,14,14,15,16,17,18,19
20,21,22,23,24,25,26,27,28,29
30,31,32,33,34,35,36,37,38,39
40,41,42,43,44,45,46,47,48,49
50
只等公子 2024-12-01 02:47:34
awk -F, '{
    for (i=1; i<=NF; i++) {
        printf("%s", $i);
        if (i % 10 == 0 || i == NF)
            printf "\n";
        else
            printf ",";
    }
}' textfile

说明:NF是字段的数量。 $i 是第 i 个字段; $ 是 Awk 中的运算符,而不是 印记

awk -F, '{
    for (i=1; i<=NF; i++) {
        printf("%s", $i);
        if (i % 10 == 0 || i == NF)
            printf "\n";
        else
            printf ",";
    }
}' textfile

Explanation: NF is the number of fields. $i is the i'th field; the $ is an operator in Awk, not a sigil.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文