regex - Bash 替换文件中的 '\n\n}' 字符串

标签 regex bash perl awk sed

我有重复包含字符串 \n\n} 的文件,我需要用 \n} 替换这样的字符串(删除两个换行符之一) .
由于此类文件是通过 bash 脚本动态生成的,因此我需要在脚本中嵌入替换代码。

我尝试了以下命令,但它不起作用:

cat file.tex | sed -e 's/\n\n}/\n}/g' # it doesn't work!
cat file.tex | perl -p00e 's/\n\n}/\n}/g' # it doesn't work!
cat file.tex | awk -v RS="" '{gsub (/\n\n}/, "\nb")}1' # it does work, but not for large files

最佳答案

您没有提供任何样本输入和预期输出,所以这是一个猜测,但也许这就是您要找的:

$ cat file
a
b

c

}
d

$ awk '/^$/{f=1;next} f{if(!/^}/)print "";f=0} 1' file
a
b

c
}
d

关于regex - Bash 替换文件中的 '\n\n}' 字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29921005/

相关文章:

linux - Bash 脚本 : How to alias Ctrl-b plus % to <F12>

perl - 我的 Linux 守护进程如何知道 Windows 程序何时停止写入我通过 SAMBA 访问的文件?

javascript - 在每个击键时仅接受 Angular js中的整数和 float

linux - 使用 Linux find 的奇怪结果

Java String Regex 分割并捕获分割部分

Bash排序,我不知道如何对同一行进行排序,并选择较旧的

windows - 在 Windows 上使用包含美元符号的字符串进行 Perl 替换

Perl:flock() 在 Linux 上运行,忽略之前在 AIX 上的锁定

android - android 中的正则表达式只接受英语

php - 如何获取MYSQL相关搜索结果