Replace One String With Another String


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace One String With Another String
# 1  
Old 02-15-2008
Replace One String With Another String

i have a file call text and i what the user to enter any word he/she wants to replace with any word

i current have

cat text

echo "Please enter the word to be amended:"
read old
echo "Please enter a new word:"
read new

sed "s/$old/$new/g" $1 > text

echo "press ENTER to continue"
read key


what is not right
# 2  
Old 02-15-2008
What is $1?
I guess you are using text as input and output , you can replace (if perl installed):

Code:
sed "s/$old/$new/g" $1 > text

with

Code:
perl -pi -e "s/$old/$new/g" text

# 3  
Old 02-15-2008
what is $1?

the script looks fine.

What's happening that's wrong? consider using set -x to see what you are doing where. Of course, [ "$1" == "text" ] && you get a problem.
# 4  
Old 02-15-2008
What if $old or $new contains a "/" char? You have to escape appropriately your variables to use them in sed regular expressions.
# 5  
Old 02-15-2008
might help to understand what it is you really mean by "what is not right"? A better question would be .... After I run the script my text file is empty or it gives me such and such results and I am looking for .... or it does nothing .... help us understand what your real problem is

I see several different potential problems .....

how do you call this script? (it is expecting a file name ... but the "cat text" always displays the file "text" )

the $1 does not necessarly have to be the file text

if you were to call this script without a file name (or with the file name of text ... you would indeed end up with an empty "text" file.

here is what I would do

change all references of "text" to "$1"

change the sed command to

cat $1 | sed s/'$old'/'$new'/g > $1

The way you wrote the sed empties the text file first then tries to put the now empty file back into itself. The cat/sed combination will empty the text file after it has done the cat.
# 6  
Old 02-15-2008
The following leaves an empty file.
Code:
cat $1 | sed s/'$old'/'$new'/g > $1
Need to direct to a tmp file and then move it back (is there a better way?)
 cat $1 | sed s/'$old'/'$new'/g > $1.$$tmp
 mv $1.$$tmp $1

Moderator's Comments:
Mod Comment Please use CODE tags when displaying sample input, output, and code segments.

Last edited by Don Cragun; 05-22-2019 at 05:33 PM.. Reason: Add CODE tags.
# 7  
Old 02-15-2008
I tried my script under ksh, bash, and sh ... all worked with the cat $1 | sed .... > $1 approach ....

Which shell were you using that resulted in an empty file?

Your approach will work, just curious as to the portability of the code if yours fails (on the questioned sed) and mine works.

Thanks
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Search partial string in a file and replace the string - UNIX

I have the below string which i need to compare with a file and replace this string in the file which matches closely. Can anyone help me on this. string(Scenario 1)- user::r--,user::ourfrd:r-- String(Scenario 2)- user::r-- File **** # file: /local/Desktop/myfile # owner: me # group:... (6 Replies)
Discussion started by: sarathy_a35
6 Replies

2. Shell Programming and Scripting

Replace string in XML file with awk/sed with string from another

Sorry for the long/weird title but I'm stuck on a problem I have. I have this XML file: </member> <member> <name>TransactionID</name> <value><string>123456789123456</string></value> </member> <member> <name>Number</name> ... (9 Replies)
Discussion started by: cozzin
9 Replies

3. UNIX for Dummies Questions & Answers

Search for a string,delete the line and replace with new string in a file

Hi Everyone, I have a requirement in ksh where i have a set of files in a directory. I need to search each and every file if a particular string is present in the file, delete that line and replace that line with another string expression in the same file. I am very new to unix. Kindly help... (10 Replies)
Discussion started by: Pradhikshan
10 Replies

4. Shell Programming and Scripting

HPUX find string in directory and filetype and replace string

Hi, Here's my dilemma. I need to replace the string Sept_2012 to Oct_2012 in all *config.py files within the current directory and below directories Is this possible? Also I am trying to find all instances of the string Sept_2012 within files in the current directory and below I have... (13 Replies)
Discussion started by: pure_jax
13 Replies

5. 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

6. Shell Programming and Scripting

replace (sed?) a string in file with multiple lines (string) from variable

Can someone tell me how I can do this? e.g: a=$(echo -e wert trewt ertert ertert ertert erttert erterte rterter tertertert ert) How do i replace the STRING with $a? I try this: sed -i 's/STRING/'"$a"'/g' filename.ext but this don' t work (2 Replies)
Discussion started by: jforce
2 Replies

7. Shell Programming and Scripting

Using sed to replace a string in file with a string in a variable that contains spaces

Hi, i call my shell like: my_shell "my project name" my script: #!/bin/bash -vx projectname=$1 sed s/'PROJECT_NAME ='/'PROJECT_NAME = '$projectname/ <test_config_doxy >temp cp temp test_config_doxy the following error occurres: sed s/'PROJECT_NAME ... (2 Replies)
Discussion started by: vivelafete
2 Replies

8. Shell Programming and Scripting

Search, replace string in file1 with string from (lookup table) file2?

Hello: I have another question. Please consider the following two sample, tab-delimited files: File_1: Abf1 YKL112w Abf1 YAL054c Abf1 YGL234w Ace2 YKL150w Ace2 YNL328c Cup9 YDR441c Cup9 YDR442w Cup9 YEL040w ... File 2: ... ABF1 YKL112W ACE2 YLR131C (9 Replies)
Discussion started by: gstuart
9 Replies

9. UNIX for Dummies Questions & Answers

Search for a string and replace the searched string in the same position in samefile

Hi All, My requisite is to search for the string "0108"(which is the year and has come in the wrong year format) in a particular column say 4th column in a tab delimited file and then replace it with 2008(the correct year format) in the same position where 0108 was found in the same file..The... (27 Replies)
Discussion started by: ganesh_248
27 Replies

10. Shell Programming and Scripting

Search for a string and replace the searched string in the same position

Hi All, My requisite is to search for the string "0108"(which is the year and has come in the wrong year format) in a particular column say 4th column in a tab delimited file and then replace it with 2008(the correct year format) in the same position where 0108 was found..The issue is the last... (15 Replies)
Discussion started by: ganesh_248
15 Replies
Login or Register to Ask a Question