How to replace partial of string in file?

Hi Guys,

I need replace part of string in a file.
for example:

ABC=123
CDE=122
DEF=456
ABC=123
DED=333
ABC=123

I need replace the value after ABC=, highlighted in red. I want to get following result;

ABC=456
CDE=122
DEF=456
ABC=456
DED=333
ABC=456

Anybody can help me this.

Thanks in advance.

For example

sed 's/ABC=.*/ABC=456/' inputfile
sed 's/\(ABC=\).*/\1456/g' file

ABC=456
CDE=122
DEF=456
ABC=456
DED=333
ABC=456
1 Like

This code works. Thanks
:b:

Be aware that since none of the solutions anchor their regular expression, if your real data can contain keys/names that are substrings of others, they might modify more lines than intended.

Regards,
Alister

1 Like

Using Perl one-liner

%perl -pe "s/ABC=\d{3}/ABC=456/g" file
ABC=456
CDE=122
DEF=456
ABC=456
DED=333
ABC=456
1 Like

Thanks for you guys reply my question. all methods are work.
I have one more question. I need get value from another file and replace the current file.
I tried

sed 's/\(ABC=\).*/\`cat oldfile`/g' file

it didn't work.
How can I fix this?

Thanks in advance.

awk -F'=' '$1=="ABC"{$2=456}1' OFS='=' file

To read from file and replace (assuming you have just one record in file: oldfile):

awk -F'=' -v V="$(cat oldfile)" '$1=="ABC"{$2=V}1' OFS='=' file
1 Like

Thanks, it works

:b: