sed find 2 strings and replace one


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers sed find 2 strings and replace one
# 1  
Old 03-14-2018
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 04:32 AM.. Reason: added code tags
# 2  
Old 03-14-2018
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.
# 3  
Old 03-14-2018
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 05:42 AM..
# 4  
Old 03-14-2018
Why the grep at all? Try
Code:
sed  '/<param name="user"/ s/CORE_BI/admin/' file

This User Gave Thanks to RudiC For This Post:
# 5  
Old 03-14-2018
Thank You RudiC Sir,

Your code works. Great.

I will try to master SED and AWK commands.
# 6  
Old 03-14-2018
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 \( \).
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to search and lossless replace strings using sed?

Hello, I would like to replace all occurencies of long data types by others (coresponding int) using 'sed' in the extensive source code of a software package written for 32 bit CPUs. I must use regular expressions to avoid wrong replacements like s/unsigned]+long/ulong/gLeaving out... (2 Replies)
Discussion started by: Mick P. F.
2 Replies

2. Shell Programming and Scripting

sed Find and Replace Text Between Two Strings or Words

I am looking for a sed in which I can recognize all of the text in between two indicators and then replace it with a place holder. For instance, the 1st indicator is a list of words "no|noone|havent" and the 2nd indicator is a list of punctuation ".|,|!".From a sentence such as "noone... (3 Replies)
Discussion started by: owwow14
3 Replies

3. Shell Programming and Scripting

Using sed to replace strings if NOT found

Dear expert, I need an urgent help. I would like to update my /etc/ntp.conf file using sed. 1) if script find this string "127.127.1.0" then add the lone below #server 127.127.1.0 2) is script find this string "fudge 127.127.1.0 stratum 10" then add #fudge 127.127.1.0 stratum 10 ... (7 Replies)
Discussion started by: lamoul
7 Replies

4. Shell Programming and Scripting

Using SED with variable to replace strings in while loop

Hi guys, Hi have this input (Menu.xml)<?xml version="1.0" encoding="ISO-8859-1"?> <breakfast_menu> <food> <name>Berry-Berry Belgian Waffles</name> <price>$8.95</price> <calories>900</calories> </food> <food> <name>French Toast</name> ... (6 Replies)
Discussion started by: cgkmal
6 Replies

5. UNIX for Advanced & Expert Users

Find and replace txt between two strings in flat file

Hi There... I need to serach and replace strngs in a text file. My file has; books.amazon='Let me read' news.bestseller='xyz' expected output is books.amazon=NONFOUND news.bestseller=NONFOUND Can I first find the text between string1= books.amazon=' and string2= ' (locate the text... (1 Reply)
Discussion started by: Hiano
1 Replies

6. UNIX for Dummies Questions & Answers

how to find and replace strings in multiple files

Hi All, Iam new to unix, I need to find string and replace it in the file name. Like text_123_0.txt,text_123_1.txt,text_123_2.txt. I need to search 123 and replace it with 234 . Is there any unix command to replace them in single command since i have 5 directories. So i need to go each and every... (0 Replies)
Discussion started by: etldeveloper
0 Replies

7. Shell Programming and Scripting

Using sed to replace two different strings?

Hey everyone! Simple question - I am trying to use sed to replace two different strings. As it stands I can implement this as: sed -i 's/TIMEOUT//g' sed -i 's/null//g' And it works. However, is it possible to shrink that down into a single command? Will there be any performance benefits? (3 Replies)
Discussion started by: msarro
3 Replies

8. Shell Programming and Scripting

Help with sed search&replace between two strings

Hi, i read couple of threads here on forum, and googled about what bugs me, yet i still can't find solution. Problem is below. I need to change this string (with sed if it is possible): This is message text that is being quoted to look like this: This is message text that is being quotedI... (2 Replies)
Discussion started by: angrybb
2 Replies

9. Shell Programming and Scripting

Replace Strings with sed or awk

Hello i need some help with the usage of sed. Situation : 2 textfiles, file.in , file.out In the first textfile which is called file.in are the words for the substitution. Every word is in a new-line like : Firstsub Secondsub Thridsub ... In the second textflie wich is called file.out is... (5 Replies)
Discussion started by: Kingbruce
5 Replies

10. Shell Programming and Scripting

AWK find/replace 2 strings in one shot

Friends, I have a file with contents like: interface Serial0/4/0/0/1/1/1/1:0 encapsulation mfr multilink group 101 interface Serial0/4/0/0/1/1/1/2:0 encapsulation ppp multilink group 101 I just have to repace mfr with ppp and ppp with mfr in a single shot. I tried using... (4 Replies)
Discussion started by: shrijith1
4 Replies
Login or Register to Ask a Question