如何在bash中用单个空行替换多个空行? [英] How can I replace multiple empty lines with a single empty line in bash?

查看:94
本文介绍了如何在bash中用单个空行替换多个空行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文件,其中包含:

I have a file that contains:

something



something else

something else again

我需要一个bash命令sed/grep,它将产生以下输出

I need a bash command, sed/grep w.e that will produce the following output

something

something else

something else again

换句话说,我需要用一个空行替换多个空行. grep/sed是基于行的.我从来没有找到可以在多行正则表达式模式下使用的BASH解决方案.

In other words, I need to replace multiple blank lines with just a single blank line. grep/sed are line based. I've never found a BASH solution that would work on multi-line regex patterns.

推荐答案

对于BSD衍生的系统(包括GNU):

For BSD-derived systems (including GNU):

您只需要cat-s选项,这会使它从其输出中删除重复的空行:

You just need cat with the -s option which causes it to remove repeated empty lines from its output:

cat -s

从手册页:-s --squeeze-blank: suppress repeated empty output lines.

这篇关于如何在bash中用单个空行替换多个空行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆