Preserved Topic: Global Find & Replace using find & perl. |
|
---|---|
Author | Thread |
Paranoid (IV) Inmate From: Minneapolis, MN, USA |
posted 07-06-2001 17:19
Okay, I am basically a Perl newbie, and not much better than that w/ shell utilities. I could look this up on my own, but if someone could just post a one-liner for me on how the syntax should look it would save me a lot of time and prevent my site from looking like crap for the next few hours . I just want to replace the regexp: |
Maniac (V) Mad Scientist From: Belgrade, Serbia |
posted 07-06-2001 19:38
http://www.meybohm.de/os/rxr.html |
Paranoid (IV) Inmate From: Minneapolis, MN, USA |
posted 07-06-2001 20:56
Thanks. That script is actually decipherable. I suppose it's time I picked up a Perl book... I keep reading that it's easier than C and Java both of which I have a very good understanding of. |
Maniac (V) Mad Scientist From: Belgrade, Serbia |
posted 07-06-2001 21:03 |
Paranoid (IV) Inmate From: Minneapolis, MN, USA |
posted 07-06-2001 22:06
Okay, I made a modification to that script so that I could filter out files that already have the change I am attempting to make... before I run it, if you would check it out to make sure I didn't make any big mistakes that are gonna fry my data I would appreciate it... code: @files=<stdin>;
|
Maniac (V) Mad Scientist From: Belgrade, Serbia |
posted 07-06-2001 22:52 |
Paranoid (IV) Inmate From: Minneapolis, MN, USA |
posted 07-06-2001 22:56
eheheh, of course not. The worst that could happen is I do a rm -r * in which case I would simply re-upload all my files |
Maniac (V) Mad Scientist From: Belgrade, Serbia |
posted 07-06-2001 23:35
He he he. Actually, I wanted to suggest that you break out from your foreach loop in the if statement that you've added, because there's no reason to go through when you set $leavealone to true (it would save a few CPU cycles), but then again if it ain't broke, don't fix it... BTW The command that will break out from loop in PERL is "last()"... |
Paranoid (IV) Inmate From: Minneapolis, MN, USA |
posted 07-06-2001 23:48
Okay, there's one last problem. How do I format the arguments? |
Maniac (V) Mad Scientist From: Belgrade, Serbia |
posted 07-07-2001 15:36
I don't have enough time to analyze what you've done, so for the sake of simplicity, I hardcoded all things directly in script, so that you don't have to call it with arguments. Also, I've fixed your regular expressions (you forgot to escape some things, like "."). |
Paranoid (IV) Inmate From: Minneapolis, MN, USA |
posted 07-09-2001 17:19
Wow, yeah, figuring out what to escape is kind of a bitch eh? I suppose passing arguments makes it even more complicated. Anyway, thanks again, I'm going to have to print out some kind of reference for this... perhaps man perlre |
Maniac (V) Mad Scientist From: Belgrade, Serbia |
posted 07-09-2001 21:50 |
Paranoid (IV) Inmate From: Minneapolis, MN, USA |
posted 07-10-2001 22:35
Is that because it interprets $ literally when it's contained within a variable? If so, would backslash backreferences not work in this case? |
Maniac (V) Mad Scientist From: Belgrade, Serbia |
posted 07-10-2001 22:52 |
Paranoid (IV) Inmate From: Minneapolis, MN, USA |
posted 07-11-2001 19:22
Hmmm, so in other words the script is totally useless for searching for regular expressions which is its goal... |
Maniac (V) Mad Scientist From: Belgrade, Serbia |
posted 07-11-2001 19:36 |
Paranoid (IV) Inmate From: Minneapolis, MN, USA |
posted 07-11-2001 21:37
No Max, that's impossible, you can't not know!!!! |
Paranoid (IV) Inmate From: Minneapolis, MN, USA |
posted 07-12-2001 19:37
hey, now I have yet another problem. Say I want to replace a 5 line block with 1 line, what approach should I use? Obviously the script I'm using here replaces line by line, so I need to search the whole file in one regexp. Do I have to concatenate the rows together or can I just refer to the @text array directly? Also I suppose I will need the s modifier to treat it as a single line to be searched. |
Nervous Wreck (II) Inmate From: |
posted 07-12-2001 20:10
perl -pi.bak -e s/foo/bar/gs `find . -name *.html -print` |
Paranoid (IV) Inmate From: Minneapolis, MN, USA |
posted 07-12-2001 22:32
Hmm, your find syntax didn't work on my linux, it doesn't let me exclude files based on regex matches, and it doesn't give any feedback to what it's doing. So somehow it doesn't seem to make up for the POS script. |