簡體   English   中英

在Linux中使用sed在兩行中匹配文本

[英]Match the text in two lines using sed in linux

我有如下模式

#error value for eg1
$text=1

我想將上述模式替換為

#error value for eg1
$text=0

如何在Linux中使用sed實現此目的?

我會怎么做:

sed '/#error value for eg1/{h;n;s/$text=1/$text=0/}' file

如果您有用戶輸入,例如您的評論似乎在說:

input=$1 # first argument of the script

# double quotes mandatory here for the variable `$input` to be evaluated :
sed "/#error value for $input/{h;n;s/\$text=1/\$text=0/}" file

像這樣:

sed -n '/^#error value for eg1$/{p;n;s/^\(\$text=\)1$/\10/};p' file

這可能對您有用(GNU sed):

sed -r 'N;s/^(#error value for eg1\n\$text=)0$/\11/;P;D' file

讀兩行,如果兩行匹配,則僅更改第二行。

暫無
暫無

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

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