linux sed模式用于在数字之后和之前插入字符]

elcex8rz  于 2023-05-06  发布在  Linux
关注(0)|答案(1)|浏览(115)

我有一个大文件如下

[0, 1, 2, 3, 4, 5, 6,    8, 9, 10               ],
    [0, 1, 2, 3, 4, 5, 6,    8, 9, 10               ],
    [0, 1, 2, 3, 4, 5, 6,    8, 9, 10               ],
    [0, 1, 2, 3, 4, 5, 6,    8, 9, 10,             11],
    [0, 1, 2, 3, 4, 5, 6,    8, 9, 10,             11],
    [0, 1, 2, 3, 4, 5, 6,    8, 9, 10,             11],

现在,我想更新整个文件,添加逗号(,)和数字(12),使其看起来如下所示

[0, 1, 2, 3, 4, 5, 6,    8, 9, 10,                 12],
    [0, 1, 2, 3, 4, 5, 6,    8, 9, 10,                 12],
    [0, 1, 2, 3, 4, 5, 6,    8, 9, 10,                 12],
    [0, 1, 2, 3, 4, 5, 6,    8, 9, 10,             11, 12],
    [0, 1, 2, 3, 4, 5, 6,    8, 9, 10,             11, 12],
    [0, 1, 2, 3, 4, 5, 6,    8, 9, 10,             11, 12],

我无法手动完成此操作,是否有更简单的sed/awk命令来完成此操作?
我尝试了sed 's/]/, 12]/g' <path_to_the_file>,但这比我想要的方式错误

[0, 1, 2, 3, 4, 5, 6,    8, 9, 10                , 12],
    [0, 1, 2, 3, 4, 5, 6,    8, 9, 10                , 12],
    [0, 1, 2, 3, 4, 5, 6,    8, 9, 10                , 12],
    [0, 1, 2, 3, 4, 5, 6,    8, 9, 10,             11, 12],
    [0, 1, 2, 3, 4, 5, 6,    8, 9, 10,             11, 12],
    [0, 1, 2, 3, 4, 5, 6,    8, 9, 10,             11, 12],

这不是我想要的结果。

kcugc4gi

kcugc4gi1#

您可以捕获]之前出现的所有空格字符

sed 's/\( *\)]/,\1 12]/'

相关问题