Forum Moderators: travelin cat

Message Too Old, No Replies

Terminal question.

Possible to do a mass search and replace in text with it?

         

Jesse_Smith

12:11 am on Oct 22, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Is there a way to have terminal do a mass text search and replace in all the files, and if possible, also in files in sub folder? For example, changing '.htm' to '.shtml'.

One command I've seen is..

awk Find and Replace text within file(s)

but that just gives out

[Jesse-Smiths-Computer: Future content] strago% awk
Usage: awk [-f programfile ¦ 'program'] [-Ffieldsep] [-v var=value] [files]

BjarneDM

11:06 pm on Oct 22, 2004 (gmt 0)

10+ Year Member



You've got two options for search and replace: awk and sed.

To see their capabilities you'll have to access the man pages for these utilities: 'man awk' and 'man sed'.

Another utility that might come in handy is xargs.

Also, you'll have to actually write a shell script. Depending on you shell (either tcsh or bash) you'll want to read 'man tcsh' or 'man bash'. For examples of what's possible see [webmasterworld.com...] . Also, in order for your script to do what you describe it'll have to be a recursive script - and they can be *very* dangerous to play with if you aren't careful.

On the other hand, if you have the means to invest in BBEdit, doing what you've described is very easy in BBEdit. As I can see from your question that you've got no experience at all using the Terminal (otherwise you would have known to use 'man awk' to get the needed info) I'll recommend investing in BBEdit. The time you'll save is more than worth the investment. Learning to use Terminal and the cli is no simple process - it's nerdy as hell and has a *very* steep learning curve.

whoisgregg

5:47 am on Oct 23, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It's not terminal, but it's easy to use:

/Library/Scripts/Finder Scripts/Replace Text in Item Names.scpt

It works from the front open window in Finder -- it's intended to be run from the Scripts menu.

timster

3:31 pm on Oct 25, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Learning to use Terminal and the cli is no simple process - it's nerdy as hell and has a *very* steep learning curve.

OK, I'll concede on nerdy, but the learning curve isn't that steep. Use the terminal a little every day and in 6 months the Linux folks may start coming to you for advice.

But I digress. Yes, BBEdit's Find and Replace will work fine for this simple replace, as will a lot of other Web development apps, such as Dreamweaver.

If you want to dip your toe into the more powerful search and replace functions of Unix, just check the "Use GREP" box in BBEdit and start reading up on Regular Expressions.

whoisgregg

4:52 pm on Oct 25, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ignore my post, I was confused by the "changing '.htm' to '.shtml'." and thought you were asking about changing file names... from the other posts, I realize now you meant making changes inside those files. Sorry! :)

Jesse_Smith

12:19 am on Oct 26, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That's OK. I also do file name changes some times!

Will BBEdit's Find and Replace do mass search and replaces from hundreds of files at once? The current program I got was made for OS9, TexFinder, so when you try to do a lot of files at once, like just a few hundred, it get's slow, since it uses Classic. One of these days I'll try to figure out that terminal stuff.

timster

1:57 pm on Oct 26, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Will BBEdit's Find and Replace do mass search and replaces from hundreds of files at once?

Yes, it should crank through that pretty quickly, assuming the files aren't huge. But you'll probably want to uncheck "Confirm Saves" and "Show Results" once you're sure it's working.

The only time I've ever notice BBEdit really slowing down is when it needs to open up really big files, and your Mac doesn't have the RAM to cope.

One of these days I'll try to figure out that terminal stuff.

If you really want to boss around your text files, learn a little Perl. (It's already installed on your Mac.

Jesse_Smith

2:58 am on Oct 27, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Is there any way to do the search and replace with out all the changed files being opened? I'm trying it out with an older version first, and yes, it get's slow when you open a lot of files, and of course you then have to save each file, unlike the OS 9 program that does the changes with out even opening the files.

timster

1:42 pm on Oct 27, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Is there any way to do the search and replace with out all the changed files being opened?

Yes. Just created 2000 files (with a quick and easy shell script) and did the replace on them in a few seconds. No files opened. I'm using BBEdit 8 but I don't recall this feature changing recently.

I think you just have to fiddle with the settings in the "Find & Replace All Matches" -- check "Save to Disk" and nothing else.

Jesse_Smith

5:57 am on Oct 28, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yep, version 7.1.4 has that. I was trying version 4.6 Lite before.

dcrombie

3:46 pm on Oct 28, 2004 (gmt 0)



Quick intro to perl (command-line) replacements:

perl -pi.old -e 's/\.html/.shtml/g' *.html

The ".old" tells perl to back-up the files first with the extension ".old" appended. eg. index.html will be backed up to index.html.old while the new index.html is modified. Leave out the ".old" if you want to live on the edge.

The "s/regexp/replacement/g" is a substitution:

s = substitute;
g = globally;
regexp = pattern to match
replacement = replacement text

*.html is the list of files to apply the substitution on (* is a wild-card in this case)

Warning! you are guaranteed to type the command wrong at least once with the result that the contents of all your files will be deleted (I must have done this a half-dozen times ;))