Computing.Net > Forums > Unix > Find and Change String in File

Find and Change String in File

Reply to Message Icon

Original Message
Name: DontKnowCrapBoutUnix
Date: February 22, 2003 at 21:45:10 Pacific
Subject: Find and Change String in File
OS: Mac OS 10.1.5
CPU/Ram: 400mhz 384 ram
Comment:

The short story is I want to find a string in a file
and change it.
Here are the details.
I'm looking for a way to change every occurence
of $source to "JavTemp" in a file called
JavTemp2.java. $source is a variable.
This is what I've got:
sed s/$source/JavTemp/g JavTemp2.java >
JavTemp.java

The problem, of course, is that sed recognizes
$source as a pattern rather than a variable.
Is there a way to make sed behave the way I want
or is there a better tact?

Thanks for any help you can offer.


Report Offensive Message For Removal


Response Number 1
Name: David Perry
Date: February 22, 2003 at 21:55:42 Pacific
Subject: Find and Change String in File
Reply: (edit)

Have you tried escaping the $ with a \ ?

sed s/\$source/JavTemp/g JavTemp2.java >
JavTemp.java


Report Offensive Follow Up For Removal

Response Number 2
Name: DontKnowCrapBoutUnix
Date: February 22, 2003 at 22:24:59 Pacific
Subject: Find and Change String in File
Reply: (edit)

Yes. It didn't work.


Report Offensive Follow Up For Removal

Response Number 3
Name: Jimbo
Date: February 22, 2003 at 23:12:01 Pacific
Subject: Find and Change String in File
Reply: (edit)

sed 's/$source/JavTemp/g' JavTemp2.java >
JavTemp.java

-jim


Report Offensive Follow Up For Removal

Response Number 4
Name: DontKnowCrapBoutUnix
Date: February 23, 2003 at 02:21:22 Pacific
Subject: Find and Change String in File
Reply: (edit)

Nope. I'm wondering if this a Mac problem.
Does this work on other systems?


Report Offensive Follow Up For Removal

Response Number 5
Name: Jimbo
Date: February 23, 2003 at 14:40:39 Pacific
Subject: Find and Change String in File
Reply: (edit)

After reading the post again, i realized that $source is a variable, not a string. If you want to perform variable substitution with sed, then enclose the expresion in double quotes.

source="some string"
sed "s/$source/JavTemp/g" JavTemp2.java >
JavTemp.java

-jim


Report Offensive Follow Up For Removal


Response Number 6
Name: DontKnowCrapBoutUnix
Date: February 23, 2003 at 15:16:11 Pacific
Subject: Find and Change String in File
Reply: (edit)

Actually, I already have it in qoutes.
source="$1"
$source gets it's value from the command line.

I've also tried a million different qouting
combinations in the sed expression to no avail.

sed "s/$source/JavTemp/g" JavTemp2.java >
JavTemp.java

sed 's/"$source"/JavTemp/g' JavTemp2.java >
JavTemp.java

sed "s/\$source/JavTemp/g" JavTemp2.java >
JavTemp.java

etc...

Maybe sed is the wrong command for this type of
thing.


Report Offensive Follow Up For Removal

Response Number 7
Name: DontKnowCrapBoutUnix
Date: February 23, 2003 at 15:23:15 Pacific
Subject: Find and Change String in File
Reply: (edit)

Oopps!!
Thank you. It works.
There was something in my Java file that was
causing problems.

Thanks a lot for the help!


Report Offensive Follow Up For Removal






Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Find and Change String in File

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




How often do you use Computing.Net?

Every Day
Once a Week
Once a Month
This Is My First Time!


View Results

Poll Finishes In 3 Days.
Discuss in The Lounge