简体   繁体   中英

Replace String using Sed?

I am new to Unix and Linux and am trying to replace a certain strings in a file using sed .

This is what I have so far:

Param1=CHANGEME 
Param2=Value2 
Param3=CHANGEME 
Param4=Value4

If I do this command :- sed -i 's/CHANGEME/VALUE/g' input.txt , "CHANGEME" will be replaced by the text "VALUE" which is fine.

What if I want to change "Value" with it's corresponding value number so essentially for Param1=Value1 and for Param3=Value3 ?

How can I achieve that? Thank you

Using GNU sed ...

Your snippet saved to hugger :

cat hugger 
Param1=CHANGEME 
Param2=value2 
Param3=CHANGEME 
Param4=value

The following sed command does what you want, I think:

sed -r '/CHANGEME/s/Param([0-9]+)=.*/Param\1=Value\1/' hugger 
Param1=Value1
Param2=value2 
Param3=Value3
Param4=value4

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