site  contact  subhomenews

Text find and replace

March 19, 2009 — BarryK
I wanted to compile the latest Homebank personal accounting package. The source package has SVG icons, but the PET package in Puppy4 has much smaller PNG icons. I did this by using Turma to do a global replacement of all occurrences of '.svg' in the sources files with '.png', and I converted all the SVG images to 16x16 PNG images. This resulted in a considerably smaller package.

However, Turma is a GTK 1.2 application and is not by default in Puppy4. So when I wanted to compile the latest Homebank, I thought that we need something in Puppy by default that can do this. Pfind only finds -- and it found that 16 files have '.svg' in them.

So, I decided to put the 'rpl' utility into Woof. This is a commandline text find and replace. Unlike sed, it replaces text directly in the file. It also handles recursive searches (into subdirectories). To make the replacement in Homebank was easy:

# cd src
# rpl -p '.svg' '.png' *.c

The '-p' causes the utility to ask for confirmation for each file it finds that needs replacement. In all, there were 68 matches.

So, 'rpl', with it's help file, will be in Woof-Puppy alpha3. What would be nice would be a GUI for it... ...I haven't got time for that though.

Home page:

Note, there is another utility called 'replace' that does the same thing, and may be slightly more sophisticated:


Username: Pizzasgood
Actually, Sed accepts a -i parameter that stands for "in-place", and when used it will indeed edit files in-place. (rpl still sounds like the better option though)

re: sed
Username: BarryK
"I never knew sed could do that! Thanks for letting me know. What about recursively descending through subdirectories to search files? ...and more than one file? So, I guess rpl is better as a batch processor? sed man page:

User perspective
Username: Sage
"Is there a plain text file, preferably accessed via a GUI menu item, that lists such powerful commands, as well as pointing to their help file? It's mythology that folks tremble at the console (apart from kiddies) because most will have used DOS daily in days of yore. But it's necessary to point to all these options from the normal UI with a one-liner [i]at that location[/i]explaining that some utilities can only be used by direct command. Providing the accompanying help file lists the switches - no problem. After all, I've always spelled 'confirmation' with a 'p' !

Username: big_bass
"I'll have to try rpl thanks for including it sed makes me dizzzzy :D so ,I use "replace" when I can because it's easy an example here cat /some_file.txt | 'old_string' 'new_string' can't get easier than that big_bass

Username: big_bass
" an example here cat /some_file.txt |[b]replace[/b] 'old_string' 'new_string' can't get easier than that ouch! did a typo above

Username: Pizzasgood
"[i]What about recursively descending through subdirectories to search files? ...and more than one file? So, I guess rpl is better as a batch processor? [/i] The simple way is like this, but it won't work with paths that have spaces: sed -i 's/\.svg/.png/g' $(find /some/path -type f) To cover spaces, you need something more like this: find /some/path -type f -exec sed -i 's/\.svg/.png/g' "{}" \; That still doesn't let you confirm, but you could probably squeeze some sort of prompt into there. Otherwise, you could split it into two parts, with the prompt and sed call as either another script or a function, and then use that script/function as the parameter for the find command.

Username: BarryK
"Puppy4 has 'replaceit', which is another, even simpler, replace utility. Can't do in-place file replacements though.

Username: B
m"About Homebank. In order for OFX records to be imported. ofxlib is to be compiled with Homebank, I think. I tried compiling separately and alltho I had a ofxlib, the import didn't work. Can you consider this?

Tags: woof