sed replace pattern

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers sed replace pattern
# 1  
Old 10-25-2016
sed replace pattern

I have a file with multiple lines, all in the same format. For each line, I need to replace the sequence of digits after the last : with a new value, but keep the single quote at the end of the line.

Example:
Input: ( two lines of file)
Code:
Name: 'text1:200/text2:1.2.3.4'
Name2: 'text3:200/text3:1.2.3.17'


New Value to use instead of 1.2.3.4:
Code:
1.2.3.x or 4.5.6.7 ( the first 3 digits don't always match)

Output:
Code:
Name: 'text1:200/text2:1.2.3.x'
Name2: 'text3:200/text3:1.2.3.x'

Can I use sed to do this?
Even if I can just replace the last digit(4) after the last dot with a new value (x) that would work for now....

Thanks!


Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 10-25-2016 at 03:02 AM.. Reason: Added CODE tags.
# 2  
Old 10-25-2016
Let the source pattern is "WINDOWS".
Target pattern is "UNIX"
File name is Pattern.txt
Then the command for replacing WINDOWS with UNIX is

1:
Code:
sed 's/WINDOWS/UNIX/' Pattern.txt

It will replace the first occurrence of WINDOWS with UNIX.

2:
Code:
sed 's/WINDOWS/UNIX/g' Pattern.txt

It will substitute the pattern globally.


Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by Madhab; 10-25-2016 at 03:11 AM.. Reason: Added CODE tags.
# 3  
Old 10-25-2016
What do you mean with
Quote:
the first 3 digits don't always match
? How and where do the 4.5.6.7 come into play?
And, why does
Quote:
New Value to use instead of 1.2.3.4
replace 1.2.3.17?

If you got a single value to replace portions of the input line, sed might do. If there's more to be done, advanced tools like awk or perl might be the better choice.
# 4  
Old 10-25-2016
Hi,

can you try the following and see if it helps ?

Code:
sed -re 's/(.*)\.[0-9]{1,2}/\1.x/' file

other way:
Code:
sed -re 's/\.[0-9]{1,2}'"'"'/.X/' file

you might need to adapt depends on your requirement if your input is different.
This User Gave Thanks to greet_sed For This Post:
# 5  
Old 10-25-2016
thanks greet_sed!
in the second option, the value I want to replace is a variable, not X
so $val=45

I tried this but doesnt resolve $val:
Code:
sed -re 's/\.[0-9]{1,2}'"'"'/.$val/' file

so I used double quotes for sed:
Code:
sed -re "s/\.[0-9]{1,2}'"'"'/.$val/" file

but there seems to a mismatch, it doesnt work wither.

I also want to write back to the same file, so
Code:
sed -i -r

should work right?

---------- Post updated at 11:41 AM ---------- Previous update was at 11:40 AM ----------

thanks greet_sed!SmilieSmilie
in the second option, the value I want to replace is a variable, not X
so $val=45

I tried this but doesnt resolve $val:
Code:
sed -re 's/\.[0-9]{1,2}'"'"'/.$val/' file

so I used double quotes for sed:
Code:
sed -re "s/\.[0-9]{1,2}'"'"'/.$val/" file

but there seems to a mismatch, it doesnt work wither.

I also want to write back to the same file, so
Code:
sed -i -r

should work right?


Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 10-25-2016 at 03:03 PM.. Reason: Added CODE tags.
# 6  
Old 10-25-2016
Hi,

Can you try the below one ?
Code:
A=45;
sed -re "s/(.*)\.[0-9]{1,2}/\1.$A/" file

use -i to do infile edit.
use -i.bak to create backup before editing.
# 7  
Old 10-25-2016
Hello Beginner101,

Could you please try following too and let me know if this helps you.
Code:
A=45;
sed -re 's/(.*)\.[0-9]{1,2}/\1.'"${A}"'/'  Input_file

Thanks,
R. Singh
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Find pattern and replace using sed

Hi, i want to replace the following lines in such a way that if the word merge exists in first column it must replace the 3rd column as M and if parse exists in first column then the last column must P, if neither it must mark it as X. I have tried the solution using awk, but it is saying... (6 Replies)
Discussion started by: charlie87
6 Replies

2. Shell Programming and Scripting

sed - Search and replace within pattern

Hi Guys! Unix newbie here! Have a requirement for which I have been scouting the forums for a solution but has been out of luck so far :( I have a file which contains the following:- TEST1|TEST2|"TEST3|1@!2"|TEST5 My sed command should result in either one the following output:-... (6 Replies)
Discussion started by: hishamzz
6 Replies

3. Shell Programming and Scripting

sed find/replace a pattern, but not this one..

I've got a file like so: ...lots of lines, etc. push "route 10.8.0.0 255.255.255.0" push "route 192.168.1.123 255.255.255.0" ...lots of lines, etc. I want to sed find/replace the IP address in the second line, whatever it is, with a new IP address, but I don't want to touch the first line.... (5 Replies)
Discussion started by: DaHai
5 Replies

4. Shell Programming and Scripting

sed command to replace two character pattern with another pattern

Not able to paste my content. Please see the attachment :-( (2 Replies)
Discussion started by: vivek d r
2 Replies

5. Shell Programming and Scripting

sed to replace pattern with filename

Hi all, I'm trying to replace a pattern/string in about 100 files with the filename using following commands but getting nowhere: for f in *.fa; do sed "s/^>.*/>$f/g" $f > $f_v1.fa; done for f in *.fa; do sed 's/^>.*/>`echo $f`/' > $fa_v1.fa; done Basically I want to change any line... (5 Replies)
Discussion started by: ivpz
5 Replies

6. Shell Programming and Scripting

Replace everything but pattern in a line using sed

I have a file with multiple lines like this: <junk><PATTERN><junk><PATTERN><junk> <junk><PATTERN><junk><PATTERN><junk><PATTERN><junk> Note that 1. There might be variable number occurrences of PATTERN in a line. 2. <> are just placeholders, they do not form part of the pattern. I need... (4 Replies)
Discussion started by: flatley
4 Replies

7. Shell Programming and Scripting

Pattern Replace using sed or awk

Hi , My file have data like 4:ALMOST NEVER PR 1925836 5:NEVER PR W DDA 5857610 6:NEVER PR WO DDA 26770205 but i want to replace the spaces before last numric digits out put should be like this 4:ALMOST NEVER PR=1925836 5:NEVER PR W DDA=5857610 6:NEVER PR WO... (7 Replies)
Discussion started by: max_hammer
7 Replies

8. Shell Programming and Scripting

How to replace the last pattern using sed?

myfile: AAAaaa BBBbbb CCCccc AAAeee DDDddd how to replace the last AAA as EEEEE using sed? like this: AAAaaa BBBbbb CCCccc EEEEEeee DDDddd (14 Replies)
Discussion started by: vistastar
14 Replies

9. Shell Programming and Scripting

SED Search Pattern and Replace with the Pattern

Hello All, I have a string "CP_STATUS OSSRC_R6_0_Shipment_R1H_CU AOM_901046 R1H_LLSV1_2008031", and I just want to extract LLSV1, but I dont get the expected result when using the sed command below. # echo "CP_STATUS OSSRC_R6_0_Shipment_R1H_CU AOM_901046 R1H_LLSV1_2008031" | awk '{print... (4 Replies)
Discussion started by: racbern
4 Replies

10. Shell Programming and Scripting

Find a pattern and replace using sed.

Hi I need to help on finding the below pattern using sed <b><a href="/home/document.do?assetkey=x-y-abcde-1&searchclause=photo"> and replace as below in the same line on the index file. <b><a href="/abcde.html"> thx in advance. Mari (5 Replies)
Discussion started by: maridhasan
5 Replies
Login or Register to Ask a Question