gpt4 book ai didi

linux - sed在bash中使用变量取消注释多行

转载 作者:太空狗 更新时间:2023-10-29 11:41:58 25 4
gpt4 key购买 nike

我遇到以下问题:我正在尝试取消对我确定使用的特定行的注释:

LINESTART=$(grep -nr "matching string" test.conf | cut -d : -f1)

之后,我需要取消注释从 $LINESTART$((LINE+10))

的代码部分
LINEEND=$((LINE+10))

我尝试了以下 sed 语法:

sed -i '${LINESTART},${LINEEND} s/# *//' test.conf

但是我得到以下错误:

sed: -e expression #1, char 4: extra characters after command

示例测试.conf:

 84     #server {
85 # listen 8000;
86 # listen somename:8080;
87 # server_name somename alias another.alias;
88
89 # location / {
90 # root html;
91 # index index.php index.html index.htm;
92 # }
93 #}

最佳答案

你能试试这个吗;

sed -i "${LINESTART},${LINEEND} s/# *//" test.conf

测试;

脚本:

#!/bin/bash
LINESTART=$(grep -nr "server {" test | cut -d : -f1 )
LINEEND=$((LINE+10))

sed "${LINESTART},${LINEEND} s/# *//" test

输出:

$ ./test.sh 
server {
listen 8000;
listen somename:8080;
server_name somename alias another.alias;

location / {
root html;
index index.php index.html index.htm;
}
}

关于linux - sed在bash中使用变量取消注释多行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41283673/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com