OT: I need to replace a certain string in a bunch of files
Hi everyone, I'm looking for a way to replace every instance of a certain text string in about nine files. What's more, what I'm looking for preferably needs to replace these items recusively, as I have the aformentioned string in a directory, and it's subdirectory. Any ideas? For some reason I have the feeling I am going to need to get to know "sed." <rant>If only I could get a version of UltraEdit for Linux, I wouldn't have this problem... :-( Unfortunately I can't find a text editor for Linux that has a multi-document interface/project interface, so it can do a "Replace in all Project files."</rant> <sigh> -Tim -- ----------------------------------------------------------------- Timothy R. Butler Universal Networks Information Tech. Consultant Christian Web Services Since 1996 ICQ #12495932 AIM: Uninettm An Authorized IPSwitch Reseller tbutler@uninetsolutions.com http://www.uninetsolutions.com ============== "Information Powered by Innovation" ==============
On Thu, Apr 26, 2001 at 09:35:09PM -0500, Timothy R.Butler wrote:
Hi everyone, I'm looking for a way to replace every instance of a certain text string in about nine files. What's more, what I'm looking for preferably needs to replace these items recusively, as I have the aformentioned string in a directory, and it's subdirectory.
Any ideas? For some reason I have the feeling I am going to need to get to know "sed." <rant>If only I could get a version of UltraEdit for Linux, I wouldn't have this problem... :-( Unfortunately I can't find a text editor for Linux that has a multi-document interface/project interface, so it can do a "Replace in all Project files."</rant>
You need ``find'' command with -exec. Find will get the files recursively, and exec will run sed to edit each file. I performed something similar against a CVS repository once, but IIRC I created a two-line wrapper script around sed (or I used vi) and called that script from -exec option of find. -Kastus
<sigh>
-Tim
Hi Kastus,
You need ``find'' command with -exec. Find will get the files recursively, and exec will run sed to edit each file. I performed something similar against a CVS repository once, but IIRC I created a two-line wrapper script around sed (or I used vi) and called that script from -exec option of find.
Thanks Kastus - I'll look into doing something like that. -Tim -- ----------------------------------------------------------------- Timothy R. Butler Universal Networks Information Tech. Consultant Christian Web Services Since 1996 ICQ #12495932 AIM: Uninettm An Authorized IPSwitch Reseller tbutler@uninetsolutions.com http://www.uninetsolutions.com ============== "Information Powered by Innovation" ==============
On Thu, Apr 26, 2001 at 09:35:09PM -0500, Timothy R.Butler wrote:
Hi everyone,
I'm looking for a way to replace every instance of a certain text string in about nine files. What's more, what I'm looking for preferably needs to replace these items recusively, as I have the aformentioned string in a directory, and it's subdirectory.
Any ideas? For some reason I have the feeling I am going to need to get to know "sed." <rant>If only I could get a version of UltraEdit for Linux, I wouldn't have this problem... :-( Unfortunately I can't find a text editor for Linux that has a multi-document interface/project interface, so it can do a "Replace in all Project files."</rant>
Alternatively, you could get to know perl. :) find . -type f -exec perl -ni.bak e 's/old text string/new string of text/g; print' {} \; (or \{\} \; at the end, to be perfectly safe) Breakdown for those unfamiliar with the command(s): finds all files that are regular files (not directories or symlinks, etc.) in the current directory (.) and executes the perl command on each file found. The perl says "On each line of a file, saving a backup in filename.bak, replace (switch, hence the 's') every occurrence of 'old text string' with 'new text string', more than once per line if you have to. Then print the line." -tara
Ok, that's just plain sexy.
Off to the bookstore to get Learning Perl!
Geordon
----- Original Message -----
From: "Tara L Andrews"
On Thu, Apr 26, 2001 at 09:35:09PM -0500, Timothy R.Butler wrote:
Hi everyone,
I'm looking for a way to replace every instance of a certain text string in about nine files. What's more, what I'm looking for preferably needs to replace these items recusively, as I have the aformentioned string in a directory, and it's subdirectory.
Any ideas? For some reason I have the feeling I am going to need to get to know "sed." <rant>If only I could get a version of UltraEdit for Linux, I wouldn't have this problem... :-( Unfortunately I can't find a text editor for Linux that has a multi-document interface/project interface, so it can do a "Replace in all Project files."</rant>
Alternatively, you could get to know perl. :)
find . -type f -exec perl -ni.bak e 's/old text string/new string of text/g; print' {} \;
(or \{\} \; at the end, to be perfectly safe)
Breakdown for those unfamiliar with the command(s): finds all files that are regular files (not directories or symlinks, etc.) in the current directory (.) and executes the perl command on each file found.
The perl says "On each line of a file, saving a backup in filename.bak, replace (switch, hence the 's') every occurrence of 'old text string' with 'new text string', more than once per line if you have to. Then print the line."
-tara
-- To unsubscribe send e-mail to suse-linux-e-unsubscribe@suse.com For additional commands send e-mail to suse-linux-e-help@suse.com Also check the FAQ at http://www.suse.com/support/faq and the archives at http://lists.suse.com
type 'perldoc' at the shell prompt.
Everything you ever wanted to know about Perl is right there on your SuSE
box (if your install didn't exclude Perl)
Or check this out: http://www.perl.org they have a good perl beginners mail
list.
HTH
John
----- Original Message -----
From: "Geordon VanTassle"
Ok, that's just plain sexy.
Off to the bookstore to get Learning Perl!
Geordon
On Thursday 26 April 2001 10:51 pm, you wrote:
Ok, that's just plain sexy.
Off to the bookstore to get Learning Perl!
That would be a good choice! And once you get through that, I highly recommend Learning Perl's big brother - Programming Perl (the Camel book). -Tim -- ----------------------------------------------------------------- Timothy R. Butler Universal Networks Information Tech. Consultant Christian Web Services Since 1996 ICQ #12495932 AIM: Uninettm An Authorized IPSwitch Reseller tbutler@uninetsolutions.com http://www.uninetsolutions.com ============== "Information Powered by Innovation" ==============
Hi Tara,
Alternatively, you could get to know perl. :)
Hmm... I'd much prefer a Perl based solution, since I'm a Perl guy already (the files that need the modifications are actually none other than components of a Perl program I am writting)
find . -type f -exec perl -ni.bak e 's/old text string/new string of text/g; print' {} \;
Say, that looks pretty good! Thanks a ton! -Tim -- ----------------------------------------------------------------- Timothy R. Butler Universal Networks Information Tech. Consultant Christian Web Services Since 1996 ICQ #12495932 AIM: Uninettm An Authorized IPSwitch Reseller tbutler@uninetsolutions.com http://www.uninetsolutions.com ============== "Information Powered by Innovation" ==============
Sorry, I could not resist:
find . -type f -exec perl -ni.bak e 's/old text string/new string of text/g; print' {} \;
$ find . -type f -exec perl -pe 's/old_string/new_string/g' {} \; is even shorter. The -p switch puts a loop-and-print around the command. The -i.bak switch is only needed when you want backup-files. As always in perl, there is more than one way to do it. Regards, Cees.
On Thu, 26 Apr 2001, Timothy R.Butler wrote:
Hi everyone, I'm looking for a way to replace every instance of a certain text string in about nine files. What's more, what I'm looking for preferably needs to replace these items recusively, as I have the aformentioned string in a directory, and it's subdirectory.
Any ideas? For some reason I have the feeling I am going to need to get to know "sed." <rant>If only I could get a version of UltraEdit for Linux, I wouldn't have this problem... :-( Unfortunately I can't find a text editor for Linux that has a multi-document interface/project interface, so it can do a "Replace in all Project files."</rant>
Then you are not looking enough. Bluefish can do this. I know that Bluefish is meant as a web editor, but it also works nicely on plain text files. Regards Ole
* Timothy R.Butler
Hi everyone, I'm looking for a way to replace every instance of a certain text string in about nine files. What's more, what I'm looking for preferably needs to replace these items recusively, as I have the aformentioned string in a directory, and it's subdirectory.
Any ideas? For some reason I have the feeling I am going to need to get to know "sed." <rant>If only I could get a version of UltraEdit for Linux, I wouldn't have this problem... :-( Unfortunately I can't find a text editor for Linux that has a multi-document interface/project interface, so it can do a "Replace in all Project files."</rant>
<sigh>
find . -exec perl -pi -e 's/ORIGINALWORD/RAPLACEMENTWORD/;' {} \; If you only want to do this in certaing files (say with a .html extension) find . -name \*.html -exec perl -pi -e 's/ORIGINALWORD/RAPLACEMENTWORD/;' {} \; If you only want to do this in files older than 10 days find . -mtime +10 -exec perl -pi -e 's/ORIGINALWORD/RAPLACEMENTWORD/;' {} \; find is your friend, and perl rules. If you feel like learning sed, don';t bother and start learning perl in stead. Kind regards, -- Gerhard den Hollander Phone +31-10.280.1515 Technical Support Jason Geosystems BV Fax +31-10.280.1511 (When calling please note: we are in GMT+1) gdenhollander@jasongeo.com POBox 1573 visit us at http://www.jasongeo.com 3000 BN Rotterdam JASON.......#1 in Reservoir Characterization The Netherlands This e-mail and any attachment is/are intended solely for the named addressee(s) and may contain information that is confidential and privileged. If you are not the intended recipient, we request that you do not disseminate, forward, distribute or copy this e-mail message. If you have received this e-mail message in error, please notify us immediately by telephone and destroy the original message.
Hi,
* Timothy R.Butler
Any ideas? For some reason I have the feeling I am going to need to get to know "sed." Perl would be an alternative.
If only I could get a version of UltraEdit for Linux, I wouldn't have this problem... :-( Unfortunately I can't find a text editor for Linux that has a multi-document interface/project interface, so it can do a "Replace in all Project files." I like UltraEdit, but I like NEdit more. They are somewhat similar, NEdit is a quite Windows-user-friendly editor, but can be very powerful.
Get the latest version from ftp.nedit.org/pub/snapshot/ and see whether the Replace dialog is what you're looking for. Thorsten
On Fri, 27 Apr 2001, Thorsten Haude wrote:
I like UltraEdit, but I like NEdit more. They are somewhat similar, NEdit is a quite Windows-user-friendly editor, but can be very powerful.
Get the latest version from ftp.nedit.org/pub/snapshot/ and see whether the Replace dialog is what you're looking for.
Or just install the nedit.rpm from series xap in Yast. Regards Ole
Moin,
* Ole Kofoed Hansen
Get the latest version from ftp.nedit.org/pub/snapshot/ and see whether the Replace dialog is what you're looking for. Or just install the nedit.rpm from series xap in Yast. The Replace dialog is new. At least on SuSE 7.0, the included NEdit is tthe release version, which does not contain it yet.
Thorsten -- Es wird dann ziemlich viel Kakao. Aber das macht nichts. Elin
participants (9)
-
Cees van de Griend
-
Geordon VanTassle
-
Gerhard den Hollander
-
John Gilger
-
Konstantin (Kastus) Shchuka
-
Ole Kofoed Hansen
-
Tara L Andrews
-
Thorsten Haude
-
Timothy R.Butler