Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

String substitution

Status
Not open for further replies.

kirk124

Programmer
Apr 15, 2003
26
US
Hi,

I use the following code to open a text file and update any strings such as "apple" to orange".

snippet:
open (IN,"c:\\data");
@lines = <IN>;
close IN;

@lines = map { s/\b$old_hostname\b/$new_hostname/sgi; $_ } @lines;
open (OUT,&quot;>c:\\data&quot;) || &quot;can't open data :$! &quot;;
print OUT @lines;

But now I must change any words such as &quot;data/applered&quot; or &quot;new_apple_dark&quot; to &quot; data/orangered&quot; &quot;new_orange_dark&quot;.

How can I alter my substitution to make it work.

--thanks kirk
 
s/data\/applered/data\/orangered/g;
s/new_apple_darl/data\/orangered/g;

&quot;Age is nothing more than an inaccurate number bestowed upon each of us at birth as just another means for others to judge and classify us- sulfericacid
 
Hi,

The strings &quot;data/applered&quot; or &quot;new_apple_dark&quot; are just example the strings might be any characters or noncharacters follow by the word apple like the following&quot;

bag/applered
bag/appleold
--/apple0123
!#&apple$%%




 
does something as simple as this work:
Code:
s/apple/orange/gi;
or are there other constraints? In your example, to specify word boundries, but didn't mention in your post, so I am a bit befuddled.

----------------------------------------------------------------------------------
...but I'm just a C man trying to see the light
 
I am trying to update a text file.
I have to substitute every word that consists of the word &quot; apple&quot; with &quot;orange&quot;.

For example:

darkapplelite tapple &&%apple098 etc becomes

darkorangelite torange &&orange098 ...


 
Well, then I think that one should do it.
Code:
$_ = 'darkapplelite tapple &&%apple098';
s/apple/orange/gi;
print;
I assume the '&&[red]%[/red]apple098' to '&&orange098' is a typo.

----------------------------------------------------------------------------------
...but I'm just a C man trying to see the light
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top