You are not logged in.
Pages: 1
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
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
Yeah, it'd be pretty straightforward with find.
find /path/to/dir -name *.html -exec sed -i "s/string1/string2/g" {} \;
Offline
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
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
Running find gives me an error: "find: paths must precede expression"
Offline
What exactly do you type in to get the error?
Offline
find /home/falcata/besm/Zim/Wiki -name *.html
find: paths must precede expression
Usage: find [-H] [-L] [-P] [path...] [expression]
Offline
Odd, I woul've put good money on that working. Try with a trailing slash?
find /home/falcata/besm/Zim/Wiki/ -name *.html
Offline
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
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
Offline
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
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
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
Well, I'm not getting anymore errors, but it didn't really do anything.
Offline
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
Oops, I forgot to change string1 and string2 after copying that command into konsole. Now it's working.
Offline
Pages: 1