Sed 在bash中的regex后面的n个非空行中添加一个字符

Sed 在bash中的regex后面的n个非空行中添加一个字符,sed,grep,Sed,Grep,我有一个包含以下行的配置文件,我想知道添加“;”的方法在[跑步机]后面的每行开始处,直到下一个空白行。我想从脚本中执行此操作,因为这些行将根据环境的配置包括在内 [treadmill] type = aor contact = server.domain.com [A_SRV] type = aor contact = serverA.domain.com [B_SRV] type = aor contact = serverB.domain.com [treadmill] type =

我有一个包含以下行的配置文件,我想知道添加“;”的方法在[跑步机]后面的每行开始处,直到下一个空白行。我想从脚本中执行此操作,因为这些行将根据环境的配置包括在内

[treadmill]
type = aor
contact = server.domain.com

[A_SRV]
type = aor
contact = serverA.domain.com

[B_SRV]
type = aor
contact = serverB.domain.com

[treadmill]
type = identify
endpoint = treadmill
match = server.domain.com

[C_SRV_IDEN]
type = identify
endpoint = sip
match = server.domain.com

[treadmill]
type = endpoint
context = LocalSets
dtmf_mode = rfc4733
disallow = all
allow = ulaw
direct_media = no
aors = treadmill

sed-e'/\[treadmill\]/,/^$/{/!s/^/;/}'treadmill.txt

说明:

  • /\[treadmill\]/,/^$/
    使用
    /START/,/END/
    语法在从开始到结束的行范围(包括开始行和结束行)中应用大括号
    {}
    中的后续命令<代码>/^$/需要一个完全空行,没有空格

  • {//!s/^/;/}
    是大括号中的一个命令,在上述范围内应用

  • /
    表示“与上一个匹配不匹配”,因为
    /
    是上一个匹配。这将阻止处理起始线和结束线

  • /!s/^//链替换
    s/^//
    //所在的行中是真的。在开始和结束之间的每行中添加分号

  • 我最初建议添加分号是
    s/*/&/-这将
    *
    替换为
    &其中替换侧的
    &
    对应于匹配的任何
    *
    。正如@williampersell所评论的,这可能比
    s/^/更不清楚/

  • 在某些shell中,
    是一个特殊字符,也需要反斜杠转义。在bash中没有替罪羊应该没问题
输出:

[treadmill]
;type = aor
;contact = server.domain.com

[A_SRV]
type = aor
contact = serverA.domain.com

[B_SRV]
type = aor
contact = serverB.domain.com

[treadmill]
;type = identify
;endpoint = treadmill
;match = server.domain.com

[C_SRV_IDEN]
type = identify
endpoint = sip
match = server.domain.com

[treadmill]
;type = endpoint
;context = LocalSets
;dtmf_mode = rfc4733
;disallow = all
;allow = ulaw
;direct_media = no
;aors = treadmill
带着awk

awk '!NF { f = 0 }
         { print (f == 1 ? ";" : "") $0 }
     $0 ~ /^\[treadmill\]$/
         { f = 1 }
' file

这是相当复杂的,但它确实工作

grep -ne "^\[treadmill\]" $DIRPATH/pjsip.conf.sample | cut -d : -f 1 | awk '{print $1" on"}' > lines.txt
grep -n '^$' $DIRPATH/pjsip.conf.sample | cut -d : -f 1 | awk '{print $1" off"}' >> lines.txt
cat lines.txt | sort -n > lines2.txt

IFS=$'\n'       # make newlines the only separator

declare -i arr4
items=0

for i in $(cat lines2.txt) ; do
    SWITCH=`echo $i | cut -d' ' -f 2`
    LINE=`echo $i | cut -d' ' -f 1`
    if [ "$SWITCH" = "on" ] && [ $SWITCHED -eq 0 ]; then
        SWITCHED=1
        arr4[$items]=$LINE
        ((++items))
    fi
    if [ "$SWITCH" = "off" ] && [ $SWITCHED -eq 1 ]; then
        SWITCHED=0
        arr4[$items]=$LINE
        ((++items))
    fi

done

count=${#arr4[@]}

let SECTIONS=$count/2
let SECTIONS-=1

for i in `seq 0 $SECTIONS` ; do
    let START=$((arr4[$i*2]))
    let END=$((arr4[($i*2)+1]))-1
    for j in `seq ${START} ${END}` ; do
        sed -i "$j s/^/;/" $DIRPATH/pjsip.conf.sample
    done
done

rm lines.txt lines2.txt

你尝试过什么,又是如何失败的?
sed'/\[跑步机\]/,/^$/{/!s/*/;&/}
@stevesliva。使用
s/^/可能稍微好一点/进行替换。做得很好。要补充信息,请参见
特殊:这通常仅适用于交互式Shell(启用历史扩展)。AFAIK,使用
“…”
(单引号字符串)中的code>在类似POSIX的shell中是安全的(
csh
是另一回事)。