sed find 2 strings and replace one

Hi Everyone,

I want to find this 2 strings in a single line a file and replace the second string.

this is the line i need to find

<param name="user" value="CORE_BI"/>

find user and CORE_BI and replace only CORE_BI with admin

so finally the line should look like this.

<param name="user" value="admin"/>

I tried this but its not working.

grep -E 'user.*CORE_BI' repository.xml | xargs sed -i "s/value=[^)]*_BI/value=\"admin\"/" repository.xml

Please tell me a solution.

Thanks In Advance.

Please wrap your code in code tags!

If you have only one file, have an if clause

if grep -E 'user.*CORE_BI' repository.xml
then
  sed -i "s/value=[^)]*_BI/value=\"admin\"/" repository.xml
fi

This takes the exit code from grep.
You can suppress the grep output.

Hi MadeInGermany,

Sir, this code make changes in another line also

Like this 2 line get changed

<param name="user" value="CORE_BI"/>
    <param name="password" value="CORE_BI"/>

in both the above line, it changes CORE_BI to admin

I want only in line where user is there.

only this line

<param name="user" value="CORE_BI"/>

Why the grep at all? Try

sed  '/<param name="user"/ s/CORE_BI/admin/' file
1 Like

Thank You RudiC Sir,

Your code works. Great.

I will try to master SED and AWK commands.

The following variant requires the _BI to follow the value= (allowing a " in between) that in turn must be located right of the 1st string.

sed -i 's/\(<param name="user".*value="\{0,1\}\)[^"]*_BI/\1admin/' file

The \1 puts back what matched in the \( \) .