sed - 如何使用 sed 取消注释第二个模式匹配上的多行?

标签 sed pattern-matching lines multiple-instances

我正在尝试使用 sed 取消注释此配置文件中的文本 block 。 我想出的代码取消了从第一个匹配的模式匹配开始并包括第一个匹配的 7 行注释,但我需要它只在第二个匹配上工作并跳过第一个匹配。

                 sed '/#location.~.*$/,+6s/#/ /' default.conf
<小时/>

# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
#    proxy_pass   http://127.0.0.1;
#}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {                
#    root           html;
#    fastcgi_pass   127.0.0.1:9000;
#    fastcgi_index  index.php;
#    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
#    include        fastcgi_params;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#

>

最佳答案

这可能对你有用(GNU sed):

sed 'x;/./{x;/#location/,+6s/#/ /;b};x;/#location/h' file

使用保持空间 (HS) 来存储标志,并且仅在设置了标志后才对地址范围起作用。

关于sed - 如何使用 sed 取消注释第二个模式匹配上的多行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15027277/

相关文章:

awk - 用新行sed替换第二个空格

ios - 与关联值匹配的 Swift 枚举模式 - 无法调用函数

java - Java 中的许多新行和奇怪的分布

查找包围点的多边形的算法 - 仅定义线

linux - 从空格后开始的行中删除所有内容

linux - 获取最新的匹配字符串值

regex - 如何仅打印与 sed 匹配的内容?

haskell - Haskell 中的元组是否严格?

postgres 中的正则表达式前瞻/后视

regex - Perl 多行匹配,同时从文件中逐行读取