You are not logged in.

#1 2008-05-23 14:07:06

Falcata
Member
From: Michiana
Registered: 2008-01-23
Posts: 501
Website

Auto-Replacement

How would I write a shell/perl/ruby/etc script to do this?

1) Scan a directory and it's subdirectories for html files
2) Scan each html file for a particular string
3) Replace all instances of that string with another.

Offline

#2 2008-05-23 14:38:41

Jack B
Member
Registered: 2006-06-27
Posts: 107
Website

Re: Auto-Replacement

I think you could do it with a single command, something along the lines of

locate /path/to/dir/*.html | xargs sed -i s/string1/string2/g

Note * will expand to include subdirectories too.

Jack

Offline

#3 2008-05-23 14:43:32

Cerebral
Forum Fellow
From: Waterloo, ON, CA
Registered: 2005-04-08
Posts: 3,108
Website

Re: Auto-Replacement

Yeah, it'd be pretty straightforward with find. 

find /path/to/dir -name *.html -exec sed -i "s/string1/string2/g" {} \;

Offline

#4 2008-05-23 14:51:24

Falcata
Member
From: Michiana
Registered: 2008-01-23
Posts: 501
Website

Re: Auto-Replacement

Neither of these seem to be working, so I think I'll clarify a little.  Zim does not have the ability to use horizontal rules.  So, in each notebook page, I insert the string "_hr_".  Now, after exporting all of the files, I want to have a script to automatically search the contents of each html file for the string "_hr_", and replace it with "<hr>", or whatever the html tag is for a horizontal ruler.

Last edited by Falcata (2008-05-23 14:51:38)

Offline

#5 2008-05-23 15:18:21

Jack B
Member
Registered: 2006-06-27
Posts: 107
Website

Re: Auto-Replacement

Cerebral's solutions is better than mine, because it only uses two programs not 3, so we'll work with his.

Which bit doesn't work? The find or the replace?  The command he gives should find the files, then execute sed on each.  Try just

find /path/to/dir -name *.html

and see if it is locating the correct files, then try

sed -i "s/_hr_/<hr>/g" filename

on one of the files, to see if the replace works.

PS - Cerebral, Why the  '{} \;' ? as far as I can tell from man find, "{}" means put the filename after sed, but "\;" is lost on me.

Jack

Offline

#6 2008-05-23 15:22:19

Falcata
Member
From: Michiana
Registered: 2008-01-23
Posts: 501
Website

Re: Auto-Replacement

Running find gives me an error: "find: paths must precede expression"

Offline

#7 2008-05-23 15:27:16

Jack B
Member
Registered: 2006-06-27
Posts: 107
Website

Re: Auto-Replacement

What exactly do you type in to get the error?

Offline

#8 2008-05-23 15:33:39

Falcata
Member
From: Michiana
Registered: 2008-01-23
Posts: 501
Website

Re: Auto-Replacement

find /home/falcata/besm/Zim/Wiki -name *.html
find: paths must precede expression
Usage: find [-H] [-L] [-P] [path...] [expression]

Offline

#9 2008-05-23 15:42:37

Jack B
Member
Registered: 2006-06-27
Posts: 107
Website

Re: Auto-Replacement

Odd, I woul've put good money on that working.  Try with a trailing slash?
find /home/falcata/besm/Zim/Wiki/ -name *.html

Offline

#10 2008-05-23 15:56:17

Falcata
Member
From: Michiana
Registered: 2008-01-23
Posts: 501
Website

Re: Auto-Replacement

No change, but I tried a little experiment.  I ran the command again, this time from my home directory (before I had been running it from the directory to search), and it ran, but displayed nothing.  Then, I ran the command from the "/" directory, and it displayed all of the files.

Offline

#11 2008-05-23 15:58:07

Jack B
Member
Registered: 2006-06-27
Posts: 107
Website

Re: Auto-Replacement

Weird... I can't explain that, but then I'm not that familiar with find.  I use locate instead.

Anyway, does the whole thing now work?

Offline

#12 2008-05-23 15:59:30

Falcata
Member
From: Michiana
Registered: 2008-01-23
Posts: 501
Website

Re: Auto-Replacement

What do I put for filename?

Offline

#13 2008-05-23 16:06:44

Jack B
Member
Registered: 2006-06-27
Posts: 107
Website

Re: Auto-Replacement

For sed on it's own you mean? One of the files, as returned by find.  Though it looks like the problem was with find, so you could just try running the command given by Cerebral from / , it should now work.

Edit: Spelling

Last edited by Jack B (2008-05-23 16:11:06)

Offline

#14 2008-05-23 16:18:51

Falcata
Member
From: Michiana
Registered: 2008-01-23
Posts: 501
Website

Re: Auto-Replacement

It's not working.  Any time I add sed on to it, I get "find: paths must precede expression"
.  I tried putting a pipe between the two commands, but instead I get this:

sed: couldn't edit /: not a regular file

Offline

#15 2008-05-23 16:23:43

nogoma
Member
From: Cranston, RI
Registered: 2006-03-01
Posts: 217

Re: Auto-Replacement

It looks like maybe your shell is doing an expansion for you, which is basically creating a filelist after the -name parameter. I would try:

find /path/to/dir -name \*.html -exec sed -i 's|string1|string2|g' {} \;

-nogoma
---
Code Happy, Code Ruby!
http://www.last.fm/user/nogoma/

Offline

#16 2008-05-23 16:30:03

Falcata
Member
From: Michiana
Registered: 2008-01-23
Posts: 501
Website

Re: Auto-Replacement

Well, I'm not getting anymore errors, but it didn't really do anything.

Offline

#17 2008-05-23 17:00:04

Cerebral
Forum Fellow
From: Waterloo, ON, CA
Registered: 2005-04-08
Posts: 3,108
Website

Re: Auto-Replacement

Falcata wrote:

Well, I'm not getting anymore errors, but it didn't really do anything.

Are you sure it didn't do anything?  If everything works, you'll get exactly zero output.

Offline

#18 2008-05-23 18:22:27

Falcata
Member
From: Michiana
Registered: 2008-01-23
Posts: 501
Website

Re: Auto-Replacement

Oops, I forgot to change string1 and string2 after copying that command into konsole.  Now it's working.

Offline

Board footer

Powered by FluxBB