且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在shell脚本中搜索多行,包括空白行

更新时间:2023-12-03 21:34:58

这对我来说适用于你的输入文件:

This works for me with your input file:

sed -n  '1h; 1!H; ${ g; s/Mary\n\n/\n/g p }'

说明:

Mary\n\n                # Mary with two consecutive newlines, the first one is automatically added by
                        # the 'H' command, and the next one means the empty line, asumming it has no
                        # spaces.
\n                      # Substitute it with a blank line.