簡體   English   中英

BSD`sed`:在shell中運行多命令`sed`腳本:替換第N個出現的位置

[英]BSD `sed`: running multi-command `sed` script from within a shell: replace Nth occurrence

我有普通的BSD SED(非GNU)。 這就是我正在運行的。 最后一個sed命令失敗。 錯誤是:字符串長於8個字符; 標簽N無效或類似的東西。 這不是完整的代碼-只是失敗的一部分:

 set -x
        chg ()
        {
        file=$1

sed ":1 
N 
$!b1 
s/)/,col1)/5 "  $file > tmp && mv tmp $file 
}

 chg $f

sed: 0602-404 Function N  cannot be parsed for both above and below

我試過了

 set -x
        chg ()
        {
        file=$1

sed ': 1 \
 N \
 $!b1 \
 s/)/,col1)/5'  $file > tmp && mv tmp $file 
}

 chg $f

相同的命令將在命令行中工作

   sed ': 1  <enter>
N  <enter> etc

你如何解決這個問題? 我想5日發生)將被代替,col )

sed -e $':x\nN;$!bx\ns/)/,col1)/5'


謝謝。 1條以上的班輪不起作用
出於某種原因,sed在線上的最后/5永遠不會在AIX上為我工作
輸入項

#more sdat
) from  ( d , bd  , c , d blah
someblah, blah ) as
somebla, lah )
as
(some other pos )
dada

輸出量

sed -e ':x' -e 'N;$!bx' -e 's/)/,col1)/5' sdat

) from  ( d , bd  , c , d blah
someblah, blah ) as
somebla, lah )
as
(some other pos )
dada

<br>

我已經成功測試了以下內容:

function chg {
  sed ':x
N 
$!bx
s/)/,col1)/5 '
}

請注意,我用雙引號替換了雙引號,並用x替換了標簽1 另外,請注意, bx后不允許有空格。

使用此腳本,以下輸入將產生如下所示的輸出:

 (a)(b)
 (c) (d) (e) (f) (g)

輸出:

(a)(b)
(c) (d) (e,col1) (f) (g)

sed使用:

 man sed | tail -n 1
 BSD                              May 10, 2005                              BSD

ps在bash中,可以將sed命令轉換為與BSD sed一起使用的單行代碼,如下所示:

sed -e $':x\nN;$!bx\ns/)/,col1)/5'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM