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

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Find and Change String in File

Reply to Message Icon

Name: DontKnowCrapBoutUnix
Date: February 22, 2003 at 21:45:10 Pacific
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.



Sponsored Link
Ads by Google

Response Number 1
Name: David Perry
Date: February 22, 2003 at 21:55:42 Pacific
Reply:

Have you tried escaping the $ with a \ ?

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


0

Response Number 2
Name: DontKnowCrapBoutUnix
Date: February 22, 2003 at 22:24:59 Pacific
Reply:

Yes. It didn't work.


0

Response Number 3
Name: Jimbo
Date: February 22, 2003 at 23:12:01 Pacific
Reply:

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

-jim


0

Response Number 4
Name: DontKnowCrapBoutUnix
Date: February 23, 2003 at 02:21:22 Pacific
Reply:

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


0

Response Number 5
Name: Jimbo
Date: February 23, 2003 at 14:40:39 Pacific
Reply:

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


0

Related Posts

See More



Response Number 6
Name: DontKnowCrapBoutUnix
Date: February 23, 2003 at 15:16:11 Pacific
Reply:

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.


0

Response Number 7
Name: DontKnowCrapBoutUnix
Date: February 23, 2003 at 15:23:15 Pacific
Reply:

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

Thanks a lot for the help!


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: Find and Change String in File

change string in multiple file www.computing.net/answers/unix/change-string-in-multiple-file/4697.html

Search and replace string in files www.computing.net/answers/unix/search-and-replace-string-in-files/7857.html

Find string in specific position www.computing.net/answers/unix/find-string-in-specific-position/5973.html