Replace string2 with string3 on all lines starting with string1

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Replace string2 with string3 on all lines starting with string1
# 1  
Old 06-23-2018
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 string2(-4.000) with string3(-2.000) on all lines starting with string1(USE solution 2, USE solution 3, …USE solution 29) but the other lines don't have to be changed.

I wrote the following script which does it, but only for the first line:

Code:
sed '/^USE solution 2/ { s,-4.000,-2.000, }' file1.txt > file2.txt

I am looking for a way to use this sed function in a loop where I could say:

If USE solution i (i=2 to 29), then do the replacement.

Being a newbie, I don't have experience at all with loops, sorry for that.

Thanks in advance.

Last edited by supernono06; 06-23-2018 at 03:59 PM..
# 2  
Old 06-23-2018
GNU sed - the one that comes with linux can handle this. When you ask a question it is a good idea to tell us the UNIX OS you have, at a minimum.

To uese extended regex (ERE) choose the sed -r option. And use alternation:

Code:
# On linux or cygwin:
 sed -r '/^USE solution ([2-9]{1}|[1][0-9]|2[0-9])/ { s,-4.000,-2.000, }'  file1.txt > file2.txt

Sample run:
Code:
$ cat file1.txt
USE solution 2; -4.000
USE solution 3; -4.000
USE solution 29; -4.000
USE solution 30; -5.000
USE solution 31; -5.000

Owner@Owner-PC ~
$ sed -r '/^USE solution ([2-9]{1}|[1][0-9]|2[0-9])/ { s,-4.000,-2.000, }'  file1.txt
USE solution 2; -2.000
USE solution 3; -2.000
USE solution 29; -2.000
USE solution 30; -5.000
USE solution 31; -5.000

# 3  
Old 06-23-2018
Thanks.

But your script does the replacement for all the lines and not only for those beginning with USE solution 2 to USE solution 29.
# 4  
Old 06-23-2018
Try this adaptation of Jim's suggestion:
Code:
sed -r '/^USE solution ([2-9]|[12][0-9]);/ { s,-4.000,-2.000, }'  file1.txt > file2.txt

These 2 Users Gave Thanks to Scrutinizer For This Post:
# 5  
Old 06-23-2018
Thanks. This works.

But how can adapt this script to apply it for e.g. lines 74 to 93?
# 6  
Old 06-24-2018
For lines 74 to 93? Or lines containing USE solution 74; till ... 93;?
# 7  
Old 06-24-2018
I meant for lines containing USE solution 74 till USE solution 93
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. Shell Programming and Scripting

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: Here is my first line Second line with string1 & string2 Not last line but it contains string1 Expected output: Here is my first line The second line with string1 &... (6 Replies)
Discussion started by: baris35
6 Replies

4. UNIX for Dummies Questions & Answers

How to grep a line not starting with # from a file (there are two lines starting with # and normal)?

e.g. File name: File.txt cat File.txt Result: #INBOUND_QUEUE=FAQ1 INBOUND_QUEUE=FAQ2 I want to get the value for one which is not commented out. Thanks, (3 Replies)
Discussion started by: Tanu
3 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 all lines except lines starting with [

Hello, I am trying to remove all the lines in file except lines starting with [ How can i accomplish this? Thank you very much in advance. (4 Replies)
Discussion started by: maxo
4 Replies

7. 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

8. Shell Programming and Scripting

Concatenate lines between lines starting with a specific pattern

Hi, I have a file such as: --- >contig00001 length=35524 numreads=2944 gACGCCGCGCGCCGCGGCCAGGGCTGGCCCA CAGGCCGCGCGGCGTCGGCTGGCTGAG >contig00002 length=4242 numreads=43423 ATGCCGAAGGTCCGCCTGGGGCTGG CGCCGGGAGCATGTAGCG --- I would like to concatenate the lines not starting with ">"... (9 Replies)
Discussion started by: s052866
9 Replies

9. 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

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