如何使用 sed 或 awk 正确查找和替换多行文本? [英] How to properly find and replace a multiline text using sed or awk?

查看:129
本文介绍了如何使用 sed 或 awk 正确查找和替换多行文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试替换以下文本时出现错误:

sed -i "s/类型脉冲后备系统默认"暗示 {显示在描述默认 ALSA 输出(当前为 PulseAudio 声音服务器)"}/类型插头slave.pcm hw/g" .asoundrc

<块引用>

未找到匹配项:(当前为 PulseAudio Sound Server)\n }/类型插头\n slave.pcm hw/g

我已经尝试使用 \ 转义 " 或将它们交换为 ' 但错误是:

<块引用>

sed:-e 表达式 #1,字符 14:未终止的 `s' 命令

问题是否与空格/新行有关?使用 sed、awk 或 perl 执行此操作的最简单方法是什么?非常感谢!

解决方案

sed 中的命令以换行符分隔.要匹配多行字符串,您可以使用 N 命令读取模式空间中的多行,然后使用 \n 正则表达式匹配它们以匹配换行符.如果未找到匹配项,则必须使用保留空间进行洗牌:保留模式空间,打印所有内容直到换行符,使用保留空间切换模式,从模式中删除所有内容直到第一个换行符,阅读下一行,重复.

那将是一些东西:

sed ': 重新开始N;N;N;N;N;# 阅读六行,我们需要那么多: 环形# 匹配六行/type pulse\n fallback "sysdefault"\n hint {\n show on\n description "Default ALSA Output (currently PulseAudio Sound Server)"\n }/{# 替换它们s//type plug\n slave.pcm hw/# 打印并重新开始;b 重启}# 保持,打印前导行,更改,删除前导行H ;s/\n.*//;;X ;s/[^\n]*\n//# 追加下一行并循环N循环'

由于编写这样的脚本很困难(对大多数人来说;),有些人只是使用 GNU sed -z 选项:

sed -z 's/type pulse\n fallback "sysdefault"\n hint {\n show on\n description "Default ALSA Output (currently PulseAudio Sound Server)"\n }/type plug\nslave.pcm hw/g'

请注意,我认为在 s 命令内的替换字符串中使用 \n 无论如何都是 GNU 扩展.

When trying to replace the following text I receive the error:

sed -i "s/  type pulse
  fallback "sysdefault"
  hint {
    show on
    description "Default ALSA Output (currently PulseAudio Sound Server)"
  }/  type plug
      slave.pcm hw/g" .asoundrc

no matches found: (currently PulseAudio Sound Server)\n }/ type plug\n slave.pcm hw/g

I've already tried to escape the " using \ or exchange them to ' but the error is:

sed: -e expression #1, char 14: unterminated `s' command

Is the issue related to the empty space/new lines? What would be the most simple way to do this using sed, awk or perl? Thank you very much!

解决方案

Commands in sed are separated by newlines. To match multiline strings, you can read multiple lines in pattern space with N command, then match them with \n regex for matching the newline. If the match is not found, you have to shuffle with hold space to: hold the pattern space, print everything up until the newline, switch pattern with hold space, remove from pattern everything up until first newline, read next line, repeat.

That would be something along:

sed '
  : restart
  N;N;N;N;N; # read six lines, we need that many
  : loop
      # match six lines
      /  type pulse\n  fallback "sysdefault"\n  hint {\n    show on\n    description "Default ALSA Output (currently PulseAudio Sound Server)"\n  }/{
           # replace them
           s//    type plug\n      slave.pcm hw/
           # print and start over
           n ; b restart
       }
       # hold, print leading line, change, remove leading line
       h ; s/\n.*// ; p ; x ; s/[^\n]*\n//
       # append next line and loop
       N
  b loop
'

As writing such scripts is hard (for most people ;), some just use GNU sed -z option:

sed -z 's/  type pulse\n  fallback "sysdefault"\n  hint {\n    show on\n    description "Default ALSA Output (currently PulseAudio Sound Server)"\n  }/  type plug\n      slave.pcm hw/g'

Note I think that using \n in replacement string inside s command is a GNU extension anyway.

这篇关于如何使用 sed 或 awk 正确查找和替换多行文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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