Computing.Net > Forums > Programming > Perl search and replace

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.

Perl search and replace

Reply to Message Icon

Name: ShaqDiesel
Date: July 27, 2006 at 17:09:00 Pacific
OS: blah
CPU/Ram: blah
Product: blah
Comment:

For example in a file if I see:
"<start> open:my_file.txt open:other_file.text </start>"
I'd like to replace it with
"<start> {open_me:my_file.txt} {open_me:other_file.text} </start>"
I try this but it doesn't match anything:
($str is the string I'm searching in)
$str =~ s/^open_me:(.*)\.(txt|text)$/{open_me:$1\.$2}/g
I used the '\' because I wanted to match a literal dot. I have the anchors to match multiple instances on the same line(like in the example). The text I want to search and replace in can be surrounded by any text, which is another reason I have the anchors. Thanks.



Sponsored Link
Ads by Google

Response Number 1
Name: FishMonger
Date: July 27, 2006 at 18:47:43 Pacific
Reply:

The ^ and $ anchors ANCHOR the match to the START and END of the string. They are not used for matching multiple instances of the pattern. Your regex will only match if you DON'T have anything before open_me or after (txt|text).

Take the anchors out.


0

Response Number 2
Name: FishMonger
Date: July 27, 2006 at 18:49:49 Pacific
Reply:

I suggest you pick up a copy of "Mastering Regular Expressions".
http://www.oreilly.com/catalog/regex/


0

Response Number 3
Name: ShaqDiesel
Date: July 27, 2006 at 20:35:20 Pacific
Reply:

It still won't work after I take out the anchors, any other suggestions?


0

Response Number 4
Name: ghostdog
Date: July 27, 2006 at 23:21:48 Pacific
Reply:

regexp is very useful if you know how to use it. Here's one that doesn't use regexp, but it's in Python. i guess you could do that too in Perl.

o = open("outputfile.txt","a")
for lines in open("inputfile.txt"):
.......if "<start>" in lines:
..............line = lines.split()
..............o.write(line[0])
..............for i in line[1:-1]:
...................o.write("{%s}" % (i) )
..............o.write(line[-1])
...
o.close()

output is <start>{open:my_file.txt}{open:other_file.text}</start>


0

Response Number 5
Name: FishMonger
Date: July 28, 2006 at 07:59:44 Pacific
Reply:

########################################################################

$str =~ s/open:my(.*?)\.(te?xt) open:other(.*?)\.(te?xt)/{open_me:$1\.$2} {open_me:$3.$4}/;

or, we should be able to simplify it to this:

s/open:(\S+)/{open_me:$1}/g;



0

Related Posts

See More



Response Number 6
Name: ShaqDiesel
Date: July 28, 2006 at 16:09:44 Pacific
Reply:

If I am using "$_=~ s/" and "/g" to search and replace multiple instances on one line, how do I extract all those replacements? Thank you.


0

Response Number 7
Name: ShaqDiesel
Date: July 28, 2006 at 16:15:40 Pacific
Reply:

I ask because I found out s/ doesnt actually go into the file and do the search and replace(like sed), so I need the new string to use File I/O and actually edit the file.


0

Response Number 8
Name: FishMonger
Date: July 28, 2006 at 17:08:24 Pacific
Reply:

I'm not sure what you mean by "how do I extract all those replacements?".

Can you post the relevant portion of your script and a reasonable sample of the before and after of the file you're trying to edit?


0

Response Number 9
Name: ShaqDiesel
Date: July 28, 2006 at 18:02:32 Pacific
Reply:

One situation(From first post):
"<start> open:my_file.txt open:other_file.text </start>"
I'd like to replace it with
"<start> {open_me:my_file.txt} {open_me:other_file.text} </start>"
Second Situation:
"<start> open:my_file.txt</start>"
I'd like to replace it with
"<start> {open_me:my_file.txt}"

I need s/ to actually edit the line inside the text file I am reading from appropriately, no matter how many matches there are in that line. So my questions are:
1. s/ doesnt affect the file you are scanning, so is additional file I/O required to search and replace stuff in the file being scanned for this regex?
2. What is the regex?
Thanks.


0

Response Number 10
Name: FishMonger
Date: July 28, 2006 at 19:07:09 Pacific
Reply:

Are you trying to do this from the command line or from a script?

You need to read the file line-by-line and pass the line to the regex, which can be done from the command line or within a script.

Here's how you can do an inline edit of your file from the command line.

perl -ni.bak -e 's/open:(\S+)/{open_me:$1}/g' somefile

There are several ways to do it within a script, here's one approach.

#!/usr/bin/perl

open (IN, "srcfile") || die $!;
open (OUT, ">destfile") || die $!;

while (<IN>) {
s/open:(\S+)/{open_me:$1}/g;
print OUT;
}
close IN;
close OUT;

Both of those methods will replace each occurrence of the match on each line (the g modifier on the regex is what allows it to make multiple matches/substitutions per line). The command line example will create a backup copy of the original file (adding the .bak ext) and the edited file retains the original filename. The second example keeps the original file intact and creates a new file with a new name of your choice.

Without seeing your source file, I can't say for sure if these will do exactly what you want/need. It is possible that the regex will need an adjustment, but given the example line you posted, each method should work.



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 Programming Forum Home


Sponsored links

Ads by Google


Results for: Perl search and replace

search and replace in DOS www.computing.net/answers/programming/search-and-replace-in-dos/14876.html

search and replace a string in ini www.computing.net/answers/programming/search-and-replace-a-string-in-ini-/16892.html

Search and Replace String in variable www.computing.net/answers/programming/search-and-replace-string-in-variable/19677.html