简体   繁体   中英

Match the text in two lines using sed in linux

I have a pattern as below

#error value for eg1
$text=1

I want to replace above pattern as

#error value for eg1
$text=0

How to achieve this using sed in linux ?

What I would do :

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

and if you have user input like your comments seems to say :

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

This might work for you (GNU sed):

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

Read two lines and only change the second if both lines match.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM