Bash - 在文件中找到连续的管道并删除多余的管道

Bash - find consecutive pipes in a file and remove the extra one

我有一个以管道作为分隔符的 txt 文件。当有 9 个连续的管道时,我想删除一个额外的管道,而它们应该只有 8 个。 我正在使用婴儿脚本来这样做。只是想通过使用正则表达式来替换那些重复的管道使其更专业。

脚本:

sed 's/|||||||||*/||||||||/g' file_1.txt > file_1_new.txt

假设您的文件中有一行如下所示,

a||||||||b||||||||e

那么你可以将其替换为,

 sed 's/\(|\{8\}\)|//g' file_1.txt > file1_new.txt

甚至:

sed -ibak 's/\(|\{8\}\)|//g' file_1.txt