我有重复包含字符串 \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/