problem with sed while replacing a string with another


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting problem with sed while replacing a string with another
# 1  
Old 05-22-2009
problem with sed while replacing a string with another

Hi,
I have a line something like this

sys,systematic,system
I want to replace only the word system with HI

I used sed for this as below
echo sys,systematic,system | sed 's/system/HI/'

but I got output as
sys,HIatic,system

I wanted output as
sys,systematic,HI

Please tell me how can I achieve desired output?

Thanks
friendyboy
# 2  
Old 05-22-2009
If file is consistent, this might work

Code:
sed 's/system/HI/2' file

# 3  
Old 05-22-2009
Code:
sed -e 's/^system,/HI,/g' -e 's/,system$/,HI/g' -e 's/^system$/HI/g' -e 's/,system,/,HI,/g'  file.txt

irrespective of "system" position the above code will work.
# 4  
Old 05-22-2009
Thanks panyam

Thanks panyam,
I was looking for similar one irrespective of the position of word "system"
I have lots of lines with such set of words.Is there any shorter way of acheiving ?

Please suggest if any

Thanks
friendyboy
# 5  
Old 05-22-2009
sed's expression set is limited. You can achieve better results with perl:

Code:
perl -p -e 's/\bsystem\b/HI/g'

The \b encoding before and after essentially tells perl to look at "word boundaries". You can achieve a similar effect in sed with [^a-zA-Z0-9-_] but you still have to take care of beginning and end-of-line situations. Perl's \b does this automatically.
# 6  
Old 05-22-2009
The perl example is a good one.

If you must do it in sed, this one is a few characters shorter than the previous example Smilie

Code:
echo sys,systematic,system | sed -e 's/^/,/' -e 's/$/,/' -e 's/,system,/,HI,/g' -e 's/^,//' -e 's/,$//'

You could also do it in awk:

Code:
echo sys,systematic,system | awk 'BEGIN {FS=OFS=","} {for (i=1;i<=NF;i++) if ($i=="system") $i="HI"; print}'

# 7  
Old 05-22-2009
Thanks canbridge.
Infact I cant use perl inside the script.

If I want the output as
sys,systematic,bigredhat\HI

where bigredhat is the system name obtained by uname -n.

what modifications need to be done in ur awk example?

Thanks
friendyboy
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed inside sed for replacing string

My need is : Want to change docBase="/something/something/something" to docBase="/only/this/path/for/all/files" I have some (about 250 files)xml files. In FileOne it contains <Context path="/PPP" displayName="PPP" docBase="/home/me/documents" reloadable="true" crossContext="true">... (1 Reply)
Discussion started by: linuxadmin
1 Replies

2. Shell Programming and Scripting

help - sed - insert space between string of form XxxAxxBcx, without replacing the pattern

If the string is of the pattern XxxXyzAbc... The expected out put from sed has to be Xxx Xyz Abc ... eg: if the string is QcfEfQfs, then the expected output is Qcf Ef Efs. If i try to substitute the pattern with space then the sed will replace the character or pattern with space,... (1 Reply)
Discussion started by: frozensmilz
1 Replies

3. Shell Programming and Scripting

problem in replacing asterisk in sed

Hi all, Sed is the one which always trobules me :( here is my input : *** it industry need to be evolved *** in the world and hope so *** to be dream the output i am expecting is : *** it industry need to be evolved *** in the world and hope so *** to be dream ... (4 Replies)
Discussion started by: panyam
4 Replies

4. Shell Programming and Scripting

error while replacing a string by new line character in sed

hi, when i am doing the following things getting error Can anyone please suggest i have a file where there is a line like the following branch=dev sdf dev jin kilii fin kale boyle dev james dev i want to search the existance of dev in the above line. cat "$file" | sed -n... (8 Replies)
Discussion started by: millan
8 Replies

5. Shell Programming and Scripting

SED: Replacing $1 with $2 escape problem

Hi all, I have a script which uses sed to replace one string with another. The problem is, the string to be matched, and its replacement are coming in as two command line arguments $1 and $2 $1 and $2 can be absolutely anything, but both should be treated purely as strings. My sed command... (7 Replies)
Discussion started by: mark007
7 Replies

6. UNIX for Dummies Questions & Answers

sed problem replacing long strings

Hi all, I have a script which uses sed to replace one string with another. The problem is, the string to be matched, and its replacement are coming in as two command line arguments $1 and $2 $1 and $2 can be absolutely anything, but both should be treated purely as strings. My sed command... (1 Reply)
Discussion started by: mark007
1 Replies

7. Shell Programming and Scripting

problem in replacing a string

Hi, I have a file with the path of the directory present somewhere in it.I have assigned the path of the directory name to a variable. I want to replace it with another string.The code i used was sed -e '/$variable/sbcd/' filename. since the path name includes slashes '/', my sed command... (9 Replies)
Discussion started by: ragavhere
9 Replies

8. Shell Programming and Scripting

Problem replacing the string

I have three files that the string inside it I want to replace so my code will be #!/bin/bash read -p "please input the old string:" string1 read -p "please input the new string:" string2 sed -i "s/string1/string2/g" *.c but the problem is.. the string that I want to replace can't be... (2 Replies)
Discussion started by: Viken
2 Replies

9. Shell Programming and Scripting

sed problem - replacement string should be same length as matching string.

Hi guys, I hope you can help me with my problem. I have a text file that contains lines like this: 78 ANGELO -809.05 79 ANGELO2 -5,000.06 I need to find all occurences of amounts that are negative and replace them with x's 78 ANGELO xxxxxxx 79... (4 Replies)
Discussion started by: amangeles
4 Replies

10. Shell Programming and Scripting

Help needed - Replacing all date & time occurrences in a file with a string using Sed

Hi, I am new to using Sed. I have a file containg lines like the following: INFORM----Test.pc:168:10/11/05 12:34:26 > some text goes here.. TRACE-----Test.pc:197:10/11/05 12:34:26 > some text goes here.. My requirement is to replace 10/11/05 12:34:26 with a string <RUNDATE> (including <... (4 Replies)
Discussion started by: Hema_M
4 Replies
Login or Register to Ask a Question