sed replace pattern

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers sed replace pattern
# 15  
Old 10-26-2016
I am confused also because my script proposal was for an input file where a number is to be substituted, as you specified in your post#1.
Now it has become a letter.?
# 16  
Old 10-26-2016
Hi guys
sorry for the confusion. Ill explain.

@MadeInGermany:
the solution you provided with pattern/newval works perfectly fine.
I can modify "pattern" to be any portion of the string in each line, and the last digit will be replaced.

THe only change right now is I want to get the "pattern" and "newval" parameters required to do the sed by parsing a third variable, instead of defining them explicitly. The main problem seems to be with evaluating the vars.

Right now if I specify everything on the command line it works:
Code:
pattern='info:200\/text\/name1:1.2.300'

-->(im escaping the / and im definning it in single quotes )
Code:
newval=8

Code:
echo " $pattern "

-> info:200\/text\/name1:1.2.300
Code:
echo " $newval "

--> 8


and call sed:
Code:
sed '/'"$pattern"\.'/ s/\(.*\.\)[0-9]\{1,\}/\1'"$newval"'/' file

input (from file):
Code:
person1_desc: 'info:200/text/name1:1.2.300.X'

output:
Code:
person1_desc: 'info:200/text/name1:1.2.300.8'

This is great. So now I will try to get the values from a new variable called $input_line

Code:
input_line=info:200/text/name1:1.2.300.8

parse it:
Code:
pattern_2=$( echo $input_line | sed 's/\.[^.]*$//' )

--> info:200/text/name1:1.2.300

Code:
newval_2=$( echo  $input_line | sed 's/.*\.//' )

--> 8

Code:
echo " $pattern_2 "

--> info:200/text/name1:1.2.300
Code:
echo "$newval_2 "

--> 8

so now if I call the exact same sed command, it doesnt work:
Code:
sed '/'"$pattern"\.'/ s/\(.*\.\)[0-9]\{1,\}/\1'"$newval"'/' file

I get:
Code:
sed: -e expression #1, char 33: extra characters after command


so thats why I want to
1. escape the slash in $pattern_2
2. not sure if I need to put $pattern_2 in single quotes?
# 17  
Old 10-26-2016
Hi,

I am sorry still it is not clear that what you are trying to achieve .

Quote:
so thats why I want to
1. escape the slash in $pattern_2
2. not sure if I need to put $pattern_2 in single quotes?
See if it helps :
Code:
pattern='info:200/text/name1:1.2.300'
pattern=$(echo $pattern | sed -e 's,/,\\/,g')
echo $pattern
info:200\/text\/name1:1.2.300

# 18  
Old 10-27-2016
I am still a bit confused: when you say .X you seem to mean that X is a number...
The / has no special meaning in an RE (=regular expression). But my sed scripts used the / delimiter. The following is with a different delimiter, #
Code:
sed '\#'"$pattern"\.'# s#\(.*\.\)[0-9]\{1,\}#\1'"$newval"'#' file

Perhaps the following is a little more precise, because it requires the search pattern to be placed left from the replacement area.
Code:
sed 's#\('"$pattern"'.*\.\)[0-9]\{1,\}#\1'"$newval"'#' file

Now you do not need to escape the / in the search pattern (instead need to escape a #).
But you *must* escape the . character! In a RE it matches any character. For example pattern="2.0/text" would find 200/text. But pattern="2\.0/text" only finds 2.0/text.
--
You want to split the string at the last dot. You can use shell-builtins for that
Code:
input_line="info:200/text/name1:1.2.300.8"
pattern_2=${input_line%.*}
echo "$pattern_2"
newval_2=${input_line##*.}
echo "$newval_2"

# 19  
Old 10-31-2016
Code:
sed 's/./3/6;s/./3/16;s/./17/24' sedd1.txt




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

Last edited by RudiC; 10-31-2016 at 05:28 AM.. Reason: Added CODE tags.
# 20  
Old 10-31-2016
Code:
# cat mytst
Name: 'text1:200/text2:1.2.3.4'
Name2: 'text3:200/text3:1.2.3.17'

Code:
# sed 's/[^:]*.$/whatever'"'"'/g' mytst
Name: 'text1:200/text2:whatever'
Name2: 'text3:200/text3:whatever'

Code:
# sed 's/[^.]*.$/whatever'"'"'/g' mytst
Name: 'text1:200/text2:1.2.3.whatever'
Name2: 'text3:200/text3:1.2.3.whatever'

@Beginner101
Note :
a) sometimes it's easier for people to understand what you need when you provide a well-chosen example of input and output file, rather than long explainations... Smilie
b) the dot before the dollar signe is useless here, but i leave it just to keep in mind that this last char matched (which is here a simple quote), is a little special and need therefore a special threatment.

---------- Post updated at 16:29 ---------- Previous update was at 13:02 ----------

Quote:
Originally Posted by Beginner101
[...]
Code:
sed: -e expression #1, char 33: extra characters after command

so thats why I want to
[...]
Yep this is due to the fact that your pattern contains some / which get parsed.

In order to avoir tedious "escaping escape" gym ... you may want to give a try using ! as reference character in the s!pattern1!pattern2! expression rather than / so that the slash will be taken as litteral.

Last edited by ctsgnb; 10-31-2016 at 12:55 PM..
This User Gave Thanks to ctsgnb For This Post:
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