regex - 删除新行,除非前面有特定字符集

标签 regex bash perl sed regex-negation

如何在 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/

相关文章:

java - 用于匹配变量和逗号之间的空格的正则表达式

python - 在 PyMongo 中查找子字符串

linux - 提供命令行参数时如何忽略目录和某些模式

bash - 删除 netstat 输出(特别是 Not all processes could be identify 行)

Perl LWP::UserAgent 无法连接到 HTTPS

ruby-on-rails - Ruby .scan 方法使用正则表达式返回空

javascript - JS - 忽略小数点后的数字

bash - bash 中的 ${1-1} 是什么意思?

perl - Tie::IxHash 在 Perl 中是如何实现的?

perl - 为什么括号只有在子声明之后才可选?