UNIX for Beginners Questions & Answers

If you're not sure where to post a Unix or Linux question, post it here. All unix and Linux beginners welcome in this forum!

sed find 2 strings and replace one


👤 Login to reply

 
Thread Tools Search this Thread Display Modes
    #1  
Old 03-14-2018
shajay12 shajay12 is offline
Registered User
 
Join Date: Mar 2018
Last Activity: 30 March 2018, 12:14 AM EDT
Posts: 15
Thanks: 2
Thanked 0 Times in 0 Posts
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
Code:
<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.
Code:
<param name="user" value="admin"/>

I tried this but its not working.
Code:
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.

Last edited by MadeInGermany; 03-14-2018 at 03:32 AM.. Reason: added code tags
Sponsored Links
    #2  
Old 03-14-2018
MadeInGermany MadeInGermany is offline Forum Staff  
Moderator
 
Join Date: May 2012
Last Activity: 19 July 2018, 2:35 AM EDT
Location: Simplicity
Posts: 4,156
Thanks: 365
Thanked 1,419 Times in 1,275 Posts
Please wrap your code in code tags!

If you have only one file, have an if clause
Code:
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.
Sponsored Links
    #3  
Old 03-14-2018
shajay12 shajay12 is offline
Registered User
 
Join Date: Mar 2018
Last Activity: 30 March 2018, 12:14 AM EDT
Posts: 15
Thanks: 2
Thanked 0 Times in 0 Posts
Hi MadeInGermany,

Sir, this code make changes in another line also

Like this 2 line get changed

Code:
<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
Code:
<param name="user" value="CORE_BI"/>


Last edited by RudiC; 03-14-2018 at 04:42 AM..
    #4  
Old 03-14-2018
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 19 July 2018, 7:29 AM EDT
Location: Aachen, Germany
Posts: 13,075
Thanks: 449
Thanked 4,016 Times in 3,693 Posts
Why the grep at all? Try
Code:
sed  '/<param name="user"/ s/CORE_BI/admin/' file

The Following User Says Thank You to RudiC For This Useful Post:
shajay12 (03-14-2018)
Sponsored Links
    #5  
Old 03-14-2018
shajay12 shajay12 is offline
Registered User
 
Join Date: Mar 2018
Last Activity: 30 March 2018, 12:14 AM EDT
Posts: 15
Thanks: 2
Thanked 0 Times in 0 Posts
Thank You RudiC Sir,

Your code works. Great.

I will try to master SED and AWK commands.
Sponsored Links
    #6  
Old 03-14-2018
MadeInGermany MadeInGermany is offline Forum Staff  
Moderator
 
Join Date: May 2012
Last Activity: 19 July 2018, 2:35 AM EDT
Location: Simplicity
Posts: 4,156
Thanks: 365
Thanked 1,419 Times in 1,275 Posts
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.
Code:
sed -i 's/\(<param name="user".*value="\{0,1\}\)[^"]*_BI/\1admin/' file

The \1 puts back what matched in the \( \).
Sponsored Links
👤 Login to reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
sed Find and Replace Text Between Two Strings or Words owwow14 Shell Programming and Scripting 3 12-07-2015 12:30 PM
Using sed to replace strings if NOT found lamoul Shell Programming and Scripting 7 05-08-2012 03:17 PM
how to find and replace strings in multiple files etldeveloper UNIX for Dummies Questions & Answers 0 02-11-2011 05:00 AM
Using sed to replace two different strings? msarro Shell Programming and Scripting 3 11-10-2010 09:56 PM
Replace Strings with sed or awk Kingbruce Shell Programming and Scripting 5 03-17-2010 03:02 AM



All times are GMT -4. The time now is 09:52 AM.

Unix & Linux Forums Content Copyrightę1993-2018. All Rights Reserved.
×
UNIX.COM Login
Username:
Password:  
Show Password





Not a Forum Member?
Forgot Password?