如何在 bash 命令行中使用 Perl 和/或 Sed 删除新行但避免使用特定字符集?
我最接近的是:
perl -C -i -p -e 's/[^.:]\n//' ~/Desktop/bak2
上面的代码在避免删除以点或冒号结尾的行方面运行良好,但它失败了,因为在删除正确的新行时它也会删除字符串的最后一个字符。我还需要将删除的\n 替换为空格。
如果可能的话,Perl 和 Sed 都有这个解决方案会很棒。 我在 perl 或 sed 中搜索了类似的解决方案,但我没有找到它,如果它确实存在,我很抱歉。
例子:
现有内容:
Violets are blue and
Buda has great teachings.
Programming can be easy because:
Stackoverflow exists,
and the community always helps
a lot.
期望的输出:
Violets are blue and Buda has great teachings.
Programming can be easy because:
Stackoverflow exists, and the community always helps a lot.
最佳答案
使用 sed
sed -e ':A;/[^.:]$/{N;bA' -e '};y/\n/ /' ~/Desktop/bak2
或 gnu sed
sed -z 's/\([^.:]\)\n/\1 /g' ~/Desktop/bak2
关于regex - 删除新行,除非前面有特定字符集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49210174/