If the \. is within the ( ) then the \1 puts it back in.
The -r is ERE in GNU sed. A Unix sed needs the standard RE or BRE:
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:
Output file:
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:
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:
If it helps, my pattern-to-look-for could also be:
Instead of just:
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.
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:
Output file:
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:
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:
If it helps, my pattern-to-look-for could also be:
Instead of just:
I hope I explained it well, sorry if I might have over done it!
Thanks!!
Moderator's Comments:
Please use CODE tags as required by forum rules!
Last edited by RudiC; 10-26-2016 at 03:56 AM..
Reason: Added CODE tags.
You best have two strings, one is the search pattern and one is the replacement value.
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:
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
and you must *not* give the trailing \..
Last edited by MadeInGermany; 10-26-2016 at 02:18 PM..
Reason: Allow longer numbers
,
Could you please explain how the sed is working? so I can understand how to make slight modificiations
Also, my input will actually be the whole line:
So based on that I need to do:
1.get pattern
2.escape the / in the pattern (right?)
3.put pattern in single quptes
4.get newval
and then call the sed
how can I extract all those values?
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)
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)
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)
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)
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)
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)
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)
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)
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)