bash - 如何在文件中用一个替换 2 个新行

标签 bash shell sed awk ash

我有以下文本文件

config 'toto'
        option 
        option 

config 'titi'
        list 
        list 

config 'tutu'
        list 
        list 

当我用 cat 显示文件时,我想将每 2 行替换为一行。

我尝试了以下命令,但它们没有用

cat file | sed -e "s@$'\n'$'\n'@$'\n'@g"
cat file | sed -e "s@\n\n@\n@g"

预期的输出是这样的:

config 'toto'
        option 
        option 
config 'titi'
        list 
        list 
config 'tutu'
        list 
        list 

最佳答案

使用sed:

sed '/^$/d' file

(或)

sed '/^[ ]*$/d' file

使用tr:

tr -s '\n' < file

关于bash - 如何在文件中用一个替换 2 个新行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22401277/

相关文章:

Linux Sed 搜索特殊字符

regex - 如果字符串包含关键字,则删除\n 换行符

bash - 如何获取管道中进程的PID

linux - 如何对指定路径下的所有文件执行 for-each 循环?

linux - 将命令结果分配给变量时 Shell 挂起

linux - 我应该对同一文件上的并行 sed 执行进行竞争条件检查吗?

python - 从许多文本文件中快速删除前 n 行

linux - 如何在 bash 脚本中删除 "-"和开头的空格?

python - MySQLdb connection.py "cant connect"与 Django

linux - Bash 脚本 -e 未检测到变量中的文件名