Issue with sed command does not replace exact string matched


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Issue with sed command does not replace exact string matched
# 1  
Old 07-12-2018
Hammer & Screwdriver Issue with sed command does not replace exact string matched

I have a file change.sed
more change.sed
Quote:
s=190.169.11.15=10.4.112.240=g
s=190.168.211.152=10.4.182.142=g
I fire the below command inorder to replace "190.169.11.15" with "10.4.112.240" in proxy.log
Code:
sed -f change.sed proxy.log

proxy.log has the below entry
more proxy.log
Quote:
including its 190.169.11.15 configuration, runtime artifacts 190.169.11.155 such as lock databases.
The command replaces both 190.169.11.15 & 190.169.11.155 as below:

Quote:
including its 10.4.112.240 configuration, runtime artifacts 10.4.112.2405 such as lock databases.
I am expecting 190.169.11.155 should not be replaced i.e if there is any extra numeric digit after the search string i.e '5' (190.169.11.155) in this case then the replace action should not happen.

Code:
bash-3.2$ uname -a
SunOS mymac 5.10 Generic_Virtual sun4v sparc sun

Can you please help.
# 2  
Old 07-12-2018
There are two problems with your regex. "." is a special character meaning "any single character", ergo, the regex "192.168.0.126" would also match "1912168Q0126"!

Fixing your particular problem depends on having a modern enough sed it supports extended regexes and backreferences.

I usually do something like this:

Code:
$ echo "word stuff 0.0.0.1 0.0.0.10 0000001 0.0.0.1" | sed 's#\(^\| \)0[.]0[.]0[.]1\($\| \)#\19.9.9.9\2#g'

word stuff 9.9.9.9 0.0.0.10 0000001 9.9.9.9

$

The red (^| ) matches either "beginning of the line" or "space", and the red \1 - just as importantly - puts it back in the output. The green does the same for end-of-line or space. Matching [.] between numbers forces it to take the . literally. '\.' probably also works but strikes me as harder to read.

Last edited by Corona688; 07-12-2018 at 12:55 PM..
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 07-12-2018
one way - not fool-proof:
Code:
s=190.169.11.15 =10.4.112.240 =g

# 4  
Old 07-12-2018
Tools

Quote:
Originally Posted by Corona688
There are two problems with your regex. "." is a special character meaning "any single character", ergo, the regex "192.168.0.126" would also match "1912168Q0126"!

Fixing your particular problem depends on having a modern enough sed it supports extended regexes and backreferences.

I usually do something like this:

Code:
$ echo "word stuff 0.0.0.1 0.0.0.10 0000001 0.0.0.1" | sed 's#\(^\| \)0[.]0[.]0[.]1\($\| \)#\19.9.9.9\2#g'

word stuff 9.9.9.9 0.0.0.10 0000001 9.9.9.9

$

The red (^| ) matches either "beginning of the line" or "space", and the red \1 - just as importantly - puts it back in the output. The green does the same for end-of-line or space. Matching [.] between numbers forces it to take the . literally. '\.' probably also works but strikes me as harder to read.
I wish to use sed -f. Also, if you can ignore the decimal issue will you be able to tell me how the simplified sed would look like where it ignore matching strings immediately followed by a numeric digit ?

---------- Post updated at 12:03 PM ---------- Previous update was at 12:00 PM ----------

Quote:
Originally Posted by vgersh99
one way - not fool-proof:
Code:
s=190.169.11.15 =10.4.112.240 =g

Sorry, i am not looking for this solution as my command runs across thousands of files. It may encounter an IP followed by any character which i m not sure.

Hence i m looking to ignore the sed whenever the matching string is followed by a numeric digit else the sed should do the replacement action..

Last edited by mohtashims; 07-12-2018 at 01:16 PM..
# 5  
Old 07-12-2018
Quote:
Originally Posted by mohtashims
I wish to use sed -f.
Then I would suggest putting the regex into a file.
# 6  
Old 07-12-2018
Quote:
Originally Posted by Corona688
Then I would suggest putting the regex into a file.
I did but it is failing as below:

more change.sed
Quote:
's=\(^\| \)190.169.11.15\($\| \)=\110.4.112.240\2=g'
Command with error output:
Code:
sed -f change.sed proxy.log
Unrecognized command: 's=\(^\| \)190.169.11.15\($\| \)=\1110.4.112.240\2=g'

# 7  
Old 07-12-2018
Remove the 'single quotes'
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Match exact String with sed command

I have a workaround to the problem i m posting, however if someone wants to look at my query and respond ... i will appreciate. This is in reference to this thread -> https://www.unix.com/shell-programming-and-scripting/267630-extract-between-two-exact-matched-strings.html I have data.txt as... (11 Replies)
Discussion started by: mohtashims
11 Replies

2. Shell Programming and Scripting

How to search and replace string in column in file with command sed?

how to search and replace string in column in file with command sed or other search "INC0000003.in" and replace column 4 = "W" $ cat file.txt INC0000001.in|20150120|Y|N|N INC0000002.in|20150120|Y|N|N INC0000003.in|20150120|Y|N|N INC0000004.in|20150120|Y|N|Noutput... (4 Replies)
Discussion started by: ppmanja3
4 Replies

3. UNIX for Dummies Questions & Answers

Replace backslash at the end of the string using sed command

I have text file which is a tab delimited one. Sample data from the file is shown below: unix is\ great\ os linux\ is superb I want to replace that backslash with empty string preserving the tab delimiter. Output should be unix is great os linux is ... (3 Replies)
Discussion started by: p.akhilreddy4u
3 Replies

4. Shell Programming and Scripting

sed or awk command to replace a string pattern with another string based on position of this string

here is what i want to achieve... consider a file contains below contents. the file size is large about 60mb cat dump.sql INSERT INTO `table1` (`id`, `action`, `date`, `descrip`, `lastModified`) VALUES (1,'Change','2011-05-05 00:00:00','Account Updated','2012-02-10... (10 Replies)
Discussion started by: vivek d r
10 Replies

5. Shell Programming and Scripting

QUESTION1: grep only exact string. QUESTION2: find and replace only exact value with sed

QUESTION1: How do you grep only an exact string. I am using Solaris10 and do not have any GNU products installed. Contents of car.txt CAR1_KEY0 CAR1_KEY1 CAR2_KEY0 CAR2_KEY1 CAR1_KEY10 CURRENT COMMAND LINE: WHERE VARIABLE CAR_NUMBER=1 AND KEY_NUMBER=1 grep... (1 Reply)
Discussion started by: thibodc
1 Replies

6. Shell Programming and Scripting

sed get matched string

Hi, how to figure out script name from a file which are having pattern .ksh. So wherever i have .ksh in a file i should get complete name of the file along with its extension as well. (7 Replies)
Discussion started by: vinsin55
7 Replies

7. Shell Programming and Scripting

How to use SED or AWK to search and replace an exact string

I have a file DS1 DDS DS I want to replace only "DS" to "DSmail.blah.com" in a lot of files. I tried sed 's/DS/DSmail.blah.com' but it changes all the lines . thanks in advance (2 Replies)
Discussion started by: gubbu
2 Replies

8. Shell Programming and Scripting

search and replace exact string

Hello Everyone, Im trying to run a search and replace of exact strings and the strings that im using variables that are passed through an array in a while loop. Here is a snip of my code: USEROLD=`cat oldusers` USERNEW=`cat newusers` USEROLDARRAY=( $USEROLD ) USERNEWARRAY=( $USERNEW )... (4 Replies)
Discussion started by: skizim
4 Replies

9. Shell Programming and Scripting

Urgent help needed !!!....to replace a exact string

Hi experts, As i am a novice unix player...so need help for the below query...banged my head from quite a while...:confused: i have a set of html files, in which i need to search for string "Page"(case sensitive) and then replace the same with some numeric code ,say, "XXX1234". Here in... (1 Reply)
Discussion started by: rahulfhp
1 Replies

10. Shell Programming and Scripting

SED: delete and print the only exact matched pattern

I am really need help with the regular expression in SED. From input file, I need to extract lines that have the port number (sport or dport) as defined. The input file is something like this time=1209515280-1209515340 dst=192.168.133.202 src=208.70.8.23 bytes=2472 proto=6 sport=80 dport=1447... (6 Replies)
Discussion started by: new_buddy
6 Replies
Login or Register to Ask a Question