sed replace pattern

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers sed replace pattern
# 8  
Old 10-25-2016
If the \. is within the ( ) then the \1 puts it back in.
Code:
sed -r 's/(.*\.)[0-9]{1,2}/\1'"$val"'/' file

The -r is ERE in GNU sed. A Unix sed needs the standard RE or BRE:
Code:
sed 's/\(.*\.\)[0-9]\{1,2\}/\1'"$val"'/' file

# 9  
Old 10-25-2016
Quote:
Originally Posted by MadeInGermany
If the \. is within the ( ) then the \1 puts it back in.
Code:
sed -r 's/(.*\.)[0-9]{1,2}/\1'"$val"'/' file

The -r is ERE in GNU sed. A Unix sed needs the standard RE or BRE:
Code:
sed 's/\(.*\.\)[0-9]\{1,2\}/\1'"$val"'/' file

Thanks!
Slight modification to the original problem:

So previously I wanted to replace the last digit before the single quote at end of line with a new value, but now I want to only do it based on the previous digits and a matching pattern.

If any(part of) line in file matches a certain pattern, replace the value after the last dot (and keep the single quote at end of line-same as before) with the value in pattern.
Can I do this using sed?


example:
Pattern to match=name1:1.2.300.Y (or could be name1:1.300.Y, so Y can be any number, I don't care what it is at this point).

All lines will be in the same format and ideally there will only be one line containing 'name1'.

Pattern to match= name1:1.2.300.Y

Original file:
Code:
person1_desc: 'info:200/text/name1:1.2.300.X'
person1_desc: 'info:200/text/name1:1.2.400.A'
person1_desc: 'info:200/text/name1:5.6.700.B'
person2_desc: 'info:200/text/name2:1.2.300.C'

Output file:
Code:
person1_desc: 'info:200/text/name1:1.2.300.Y'
person1_desc: 'info:200/text/name1:1.2.400.A'
person1_desc: 'info:200/text/name1:5.6.700.B'
person2_desc: 'info:200/text/name2:1.2.300.C'

So only the first line was a match and updated, 'X' was replaced with 'Y'. Line 4 wasn't changed( name2)



Pattern I'm looking for:
Code:
name1:1.2.300.Y

In other words:
{string}:{ a combination of one or more numbers and dots, not always 2 dots, but grouped together}.{one or more digits}

Or if I could also extract each field above from the original file into variables:
Code:
Var1=name1
Var2=1.2.300
Var3=Y

If it helps, my pattern-to-look-for could also be:
Code:
info:200/text/name1:1.2.300.Y

Instead of just:
Code:
name1:1.2.300.Y


I hope I explained it well, sorry if I might have over done it!
Thanks!!

---------- Post updated at 10:49 PM ---------- Previous update was at 10:48 PM ----------

Quote:
Originally Posted by RavinderSingh13
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

Yes that works, thank you!
Slight modification to original problem:


previously I wanted to replace the last digit before the single quote at end of line with a new value, but now I want to only do it based on the previous digits and a matching pattern.

If any(part of) line in file matches a certain pattern, replace the value after the last dot (and keep the single quote at end of line-same as before) with the value in pattern.
Can I do this using sed?


example:
Pattern to match= name1:1.2.300.Y (or could be name1:1.300.Y, so Y can be any number, I don't care what it is at this point).

All lines will be in the same format and ideally there will only be one line containing 'name1'.

Pattern to match= name1:1.2.300.Y

Original file:
Code:
person1_desc: 'info:200/text/name1:1.2.300.X'
person1_desc: 'info:200/text/name1:1.2.400.A'
person1_desc: 'info:200/text/name1:5.6.700.B'
person2_desc: 'info:200/text/name2:1.2.300.C'

Output file:
Code:
person1_desc: 'info:200/text/name1:1.2.300.Y'
person1_desc: 'info:200/text/name1:1.2.400.A'
person1_desc: 'info:200/text/name1:5.6.700.B'
person2_desc: 'info:200/text/name2:1.2.300.C'

So only the first line was a match and updated, 'X' was replaced with 'Y'. Line 4 wasn't changed( name2)



Pattern I'm looking for:
Code:
name1:1.2.300.Y

In other words:
{string}:{ a combination of one or more numbers and dots, not always 2 dots, but grouped together}.{one or more digits}

Or if I could also extract each field above from the original file into variables:
Code:
Var1=name1
Var2=1.2.300
Var3=Y

If it helps, my pattern-to-look-for could also be:
Code:
info:200/text/name1:1.2.300.Y

Instead of just:
Code:
name1:1.2.300.Y


I hope I explained it well, sorry if I might have over done it!
Thanks!!




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

Last edited by RudiC; 10-26-2016 at 03:56 AM.. Reason: Added CODE tags.
# 10  
Old 10-26-2016
PLEASE exercise some care when editing a post NOT to duplicate its contents!
# 11  
Old 10-26-2016
You best have two strings, one is the search pattern and one is the replacement value.
Code:
#!/bin/sh
pattern=$1
newval=$2
if [ $# -ne 2 ]; then
  echo "usage: $0 'RE pattern' newval"
  exit 1
fi
sed 's/\(.*'"$pattern"'\.\)[0-9]\{1,\}/\1'"$newval"'/' file

Here the given pattern must reach to the last . (before the last number). For example you can run your script with
./scriptname 'name1:1\.2\.300' 199 or ./scriptname ':1\.2\.300' 199 but not ./scriptname 'name1:1\.2\.' 199.
The following modification searches the pattern in the whole line:
Code:
sed '/'"$pattern"'/ s/\(.*\.\)[0-9]\{1,\}/\1'"$newval"'/' file

This requires you to end the given pattern with a \. (otherwise there is a risk you match the first part of the number).
As an alternative you can put the trailing \. into the sed code
Code:
sed '/'"$pattern"\.'/ s/\(.*\.\)[0-9]\{1,\}/\1'"$newval"'/' file

and you must *not* give the trailing \..

Last edited by MadeInGermany; 10-26-2016 at 02:18 PM.. Reason: Allow longer numbers
# 12  
Old 10-26-2016
Hi,

Can you please try and adapt if your input/output changes ?

Code:
cat file
person1_desc: 'info:200/text/name1:1.2.300.X'
person1_desc: 'info:200/text/name1:1.2.400.A'
person1_desc: 'info:200/text/name1:5.6.700.B'
person2_desc: 'info:200/text/name2:1.2.300.C'
person2_desc: 'info:200/text/name1:1.298.X'

Code:
sed -re 's/([a-z]+1:[0-9].[0-9]?.?[0-9]{3}.)(X)/\1Y/' file

Quote:
Gives output:
person1_desc: 'info:200/text/name1:1.2.300.Y'
person1_desc: 'info:200/text/name1:1.2.400.A'
person1_desc: 'info:200/text/name1:5.6.700.B'
person2_desc: 'info:200/text/name2:1.2.300.C'
person2_desc: 'info:200/text/name1:1.298.Y'

Last edited by greet_sed; 10-26-2016 at 09:09 AM.. Reason: Add text.
# 13  
Old 10-26-2016
Thanks
Quote:
MadeInGermany
,
Could you please explain how the sed is working? so I can understand how to make slight modificiations Smilie

Also, my input will actually be the whole line:
Code:
info:200/text/name1:1.2.300.Y

So based on that I need to do:

1.get pattern
Code:
pattern=info:200/text/name1:1.2.300

2.escape the / in the pattern (right?)
Code:
pattern=info:200\/text\/name1:1.2.300

3.put pattern in single quptes
Code:
pattern='info:200\/text\/name1:1.2.300'

4.get newval
Code:
newval=Y

and then call the sed
how can I extract all those values?


Thanks again!
# 14  
Old 10-26-2016
Hi,
Did you try the code posted in my previous post ?

"how can I extract all those values"
What do you mean by that ?
Please provide input & expected output.
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