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
# 8  
Old 07-12-2018
Quote:
Originally Posted by Corona688
Remove the 'single quotes'
Error is gone but it Did not help. Nothing got replaced when I was expecting the first (..its 190.169.11.15 c..) and third (.. >190.169.11.15< ...) occurrences of 190.169.11.15 to get replaced and only the second one (... 190.169.11.155 ...)should have been ignored.

more proxy.log
Quote:
including its 190.169.11.15 configuration, runtime artifacts 190.169.11.155 such as >190.169.11.15< lock databases.
more change.sed
Quote:
s=\(^\| \)190.169.11.15\($\| \)=\110.4.112.240\2=g
command: sed -f change.sed proxy.log

Last edited by mohtashims; 07-12-2018 at 01:51 PM..
# 9  
Old 07-12-2018
That it worked partially is very strange. Show the input you had, the output you were expecting, the output you got, and the exact code and regex file you used.
# 10  
Old 07-12-2018
Hammer & Screwdriver

Quote:
Originally Posted by Corona688
That it worked partially is very strange. Show the input you had, the output you were expecting, the output you got, and the exact code and regex file you used.
Input or original file proxy.log
Quote:
including its 190.169.11.15 configuration, runtime artifacts 190.169.11.155 such as >190.169.11.15< lock databases.
Expected output:
Quote:
including its 10.4.112.240 configuration, runtime artifacts 190.169.11.155 such as >10.4.112.240< lock databases.
as you can see only 2/3 string "190.169.11.15" should be replaced.

The one which should not get replaced is 190.169.11.155 because it is followed by / preceded by a numeric digit(see red bold)

Current i am getting the below output:
Quote:
including its 190.169.11.15 configuration, runtime artifacts 190.169.11.155 such as >190.169.11.15< lock databases.
As you can see nothing got replaced Smilie

more change.sed
Code:
s=\(^\| \)190.169.11.15\($\| \)=\110.4.112.240\2=g

My command is sed -f change.sed proxy.log
# 11  
Old 07-12-2018
Ah, I think I see:

I specified "beginning of line or space", not "beginning of line, space, or >". I can specify "beginning of line or non-numeric", "end of line or non-numeric" instead.

How about:

Code:
s=\(^\|[^0-9]\)190.169.11.15\($\|[^0-9]\)=\110.4.112.240\2=g

# 12  
Old 07-12-2018
Quote:
Originally Posted by Corona688
Ah, I think I see:

I specified "beginning of line or space", not "beginning of line, space, or >". I can specify "beginning of line or non-numeric", "end of line or non-numeric" instead.

How about:

Code:
s=\(^\|[^0-9]\)190.169.11.15\($\|[^0-9]\)=\110.4.112.240\2=g

This also does not work. It does not replace anything. See output:
Code:
echo " logs for the >190.169.11.15< instances on various nodes. If there 190.169.11.1555 are no instan" | sed 's=\(^\|[^0-9]\)190.169.11.15\($\|[^0-9]\)=\110.4.112.240\2=g'
 logs for the >190.169.11.15< instances on various nodes. If there 190.169.11.1555 are no instan

# 13  
Old 07-12-2018
Moderator's Comments:
Mod Comment Posting "Does not work" without explanation does not help you or anyone. If a command does not work for you, please show the exact circumstances you used it, and the exact error or malfunction you received. Do not paraphrase errors, or post the text as links, images, or attachments if you can avoid it: Paste the exact message, in code tags, like [code] text [/code] or by selecting the text and using the Image button.

Thank you.

The UNIX and Linux Forums


You have sneak-edited in more details now but it should have had them in the first place.

It works perfectly with the input you gave me, I get
Code:
including its 10.4.112.240 configuration, runtime artifacts 190.169.11.155 such as >10.4.112.240< lock databases.

Show exactly how you used it down to the last keystroke.
# 14  
Old 07-12-2018
Hammer & Screwdriver

Quote:
Originally Posted by Corona688
Moderator's Comments:
Mod Comment Posting "Does not work" without explanation does not help you or anyone. If a command does not work for you, please show the exact circumstances you used it, and the exact error or malfunction you received. Do not paraphrase errors, or post the text as links, images, or attachments if you can avoid it: Paste the exact message, in code tags, like [code] text [/code] or by selecting the text and using the Image button.

Thank you.

The UNIX and Linux Forums


You have sneak-edited in more details now but it should have had them in the first place.

It works perfectly with the input you gave me, I get
Code:
including its 10.4.112.240 configuration, runtime artifacts 190.169.11.155 such as >10.4.112.240< lock databases.

Show exactly how you used it down to the last keystroke.
I am sharing the snapshot so there is no doubt
Image
Image
CLICK HERE to see SNAPSHOT
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