Replace string2 by string3 where string1 is found in line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace string2 by string3 where string1 is found in line
# 1  
Old 08-26-2018
Replace string2 by string3 where string1 is found in line

Hello,
My aim is to search string1 in all lines. When found, find and replace string2 by string3 if possible.

TextFile:
Code:
Here is my first line
Second line with string1 & string2
Not last line but it contains string1

Expected output:
Code:
Here is my first line
The second line with string1 & string3
Not last line but it contains string1

I tested below command:
Code:
grep "string1" TextFile | xargs sed -i 's/string2/string3/g'


I feel really lucky if you would shed light on this question.

Many thanks
Boris

Last edited by baris35; 09-05-2018 at 06:17 PM..
# 2  
Old 08-26-2018
With your grep pipeline, any line in TextFile containing the string string1 will be treated by xargs as the name of a file to be passed as an operand to sed. I fail to see how that would meet your ambiguous requirements no matter how I try to guess at what you mean.

And, since you haven't told us what operating system and shell you're using, we have no way of knowing whether or not the non-standard sed -i option is supported on your system.

If sed on your system does have a -i option and what you want to do is replace every occurrence of the string string2 with the string string3 on every line that contains the string string1 and leave lines that do not contain the string string1 unchanged, the obvious way to do what you want is:
Code:
sed -i '/string1/s/string2/string3/g' TextFile

and a safer, portable way to do that would be to use:
Code:
sed '/string1/s/string2/string3/g' "TextFile" > "TextFile.$$" && cp "TextFile.$$" "Textfile"; rm -f "TextFile.$$"

# 3  
Old 08-26-2018
Code:
awk ' /string1/ {gsub (/string2/, "string3")} 1' file

# 4  
Old 08-26-2018
Dear Don,
Thanks for your reply.
I am running under ubuntu 18.04.
Let me specificially give an example for my case:

Let's say, string1 is keyword, string2 is .
I wish to remove dot sign when keyword is found in line:
I do:
Code:
sed '/keyword/s/.//g' TextFile > output

Double quotes is also not sorting it out.

There is no below line in output file:
Code:
Second line with keyword &.

# 5  
Old 08-26-2018
You can try like this:
Code:
STR1="string1"
STR2="string2"
STR3="string3"
awk '$0 ~ STR1 {gsub (STR2, STR3); } 1' STR1="$STR1" STR2="$STR2" STR3="$STR3"  file


However, if your search string2 has a regex special character in it, you need to escape it, like

Code:
STR1="keyword"
STR2="\."
STR3=""

# 6  
Old 08-26-2018
Quote:
Originally Posted by baris35
Dear Don,
Thanks for your reply.
I am running under ubuntu 18.04.
Let me specificially give an example for my case:

Let's say, string1 is keyword, string2 is .
I wish to remove dot sign when keyword is found in line:
I do:
Code:
sed '/keyword/s/.//g' TextFile > output

Double quotes is also not sorting it out.

There is no below line in output file:
Code:
Second line with keyword &.

Of course there isn't. In the command:
Code:
sed '/keyword/s/.//g'

keyword and . are not just strings, they are regular expressions. And the regular expression . matches any single character. And globally replacing every character in a line with an empty string changes every line that contains keyword into an empty line.

To match a literal period character, try:
Code:
sed '/keyword/s/\.//g' TextFile > output

or:
Code:
sed '/keyword/s/[.]//g' TextFile > output

This User Gave Thanks to Don Cragun For This Post:
# 7  
Old 08-26-2018
Hello Don,
Voila...
Both gives the expected output.


Thanks a lot!
Boris
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Replace string2 by string3 where string1 is found in line

I found this in the forum that searches a file for string1, substitute all occurrences of string2 with string3. (Title: Replace string2 by string3 where string1 is found in line) >> sed -i '/string1/s/string2/string3/g' TextFile How will I perform the same sed command and only substitute... (3 Replies)
Discussion started by: apalex
3 Replies

2. Shell Programming and Scripting

Find string1, when true find string2 in reverse direction

Hello, This is a bit complicated for me. My scenario in MyFile: Search string1, When string1 is found, grep the line containing string1, go back over that line in upward direction and grep the first line containing string2. Here is an example: MyFile His email address... (17 Replies)
Discussion started by: baris35
17 Replies

3. UNIX for Beginners Questions & Answers

Replace string2 with string3 on all lines starting with string1

My OS is Windows 10 and I am using Cygwin. The file 1 content is: USE solution 2; -4.000 USE solution 3; -4.000 … USE solution 29; -4.000 USE solution 30; -4.000 USE solution 31; -4.000 …. USE solution 89; -4.000 ... USE solution 202; -4.000 etc... I need to replace... (8 Replies)
Discussion started by: supernono06
8 Replies

4. Shell Programming and Scripting

Read file and replace a particular line if found

Hi All There is another challenge which stand in front of me. And want all to have the experience with that I have a file in Unix say a.txt. What I was trying is to read the file line by line and matching the line to particular pattern, and if that pattern found I want to replace that line... (5 Replies)
Discussion started by: adisky123
5 Replies

5. Shell Programming and Scripting

awk - print record with both string1 and string2

How do I use awk to find the records in a file that contains two specific strings? I have tried piping and using awk two times, but I don't know how to do it in one action. (2 Replies)
Discussion started by: locoroco
2 Replies

6. Shell Programming and Scripting

Remove lines before string1 and after string2

Hello All... I have a text file (.ics) which I need to read into a variable but ONLY the part including and after 'BEGIN:VEVENT' and ending with END:VEVENT Anything before BEGIN:VEVENT or after END:VEVENT should be ignored. Thanks for input Jeff BEGIN:VCALENDAR VERSION:2.0... (3 Replies)
Discussion started by: uptimejeff
3 Replies

7. UNIX for Dummies Questions & Answers

Replace line with found unknown pattern

Hi, I have a file with the following content: --------- a 3242 tc_5 gdfg4 random text a 3242 tc_6 gdfg4 random text a 3242 tc_7 gdfg4 random text a 3242 tc_4 gdfg4 --------- I want to replace the lines containing tc_? (tc_5, tc_6 etc. even with unknown numbers) with the found... (5 Replies)
Discussion started by: joas
5 Replies

8. Shell Programming and Scripting

ps -ef | grep "string1" "string2" " "string3"

Hi all, can any one suggest me the script to grep multiple strings from ps -ef pls correct the below script . its not working/ i want to print OK if all the below process are running in my solaris system. else i want to print NOT OK. bash-3.00$ ps -ef | grep blu lscpusr 48 42 ... (11 Replies)
Discussion started by: steve2216
11 Replies

9. Shell Programming and Scripting

How to replace a line below where the pattern found

Hi All, I have a file say abc.xml. In this file, I need to search for a pattern “SAP_GATEWAY_HOST”; if this pattern found and the next line also contain the pattern “nwprc03.cos” then I need to replace this pattern “nwprc03.cos” with some other pattern “nwdrc03.apjp”. $ cat abc.xml... (3 Replies)
Discussion started by: Ritesh.patni84
3 Replies

10. UNIX for Dummies Questions & Answers

Replacing string1 with string2 in many files

I have 70 files and want to replace string1 with string2. How can i do that?. Thanks (4 Replies)
Discussion started by: shashikandi
4 Replies
Login or Register to Ask a Question