使用sed匹配包含换行符的字符串 [英] Match a string that contains a newline using sed

查看:1417
本文介绍了使用sed匹配包含换行符的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像这样的字符串:

I have a string like this one:

    #
    pap

基本上翻译为\t#\n\tpap,我想将其替换为:

which basically translates to a \t#\n\tpap and I want to replace it with:

    #
    pap
    python

转换为\t#\n\tpap\n\tpython.

在很多方面与sed进行了尝试,但是它不起作用可能是因为sed以不同的方式使用换行符.我尝试过:

Tried this with sed in a lot of ways but it's not working maybe because sed uses new lines in a different way. I tried with:

sed -i "s/\t#\n\tpap/\t#\tpython\n\tpap/" /etc/freeradius/sites-available/default

...以及许多其他方式都没有结果.知道在这种情况下我该如何更换吗?

...and many different other ways with no result. Any idea how can I do my replace in this situation?

推荐答案

使用gawk尝试以下行:

try this line with gawk:

awk -v RS="\0" -v ORS="" '{gsub(/\t#\n\tpap/,"yourNEwString")}7' file

如果要让sed处理新行,则必须先阅读整个文件:

if you want to let sed handle new lines, you have to read the whole file first:

sed ':a;N;$!ba;s/\t#\n\tpap/NewString/g' file

这篇关于使用sed匹配包含换行符的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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