Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

uzapuca

macrumors newbie
Original poster
Sep 15, 2006
8
0
Hi guys
Is there any script, automator function or Batch Rename program that you might know which remove

1) space between words and put a "_" hyphen instead
2) remove all non standard web characters (for a linux server) like written accents, characters is other language like "ñ", etc?

I am doing them by hand and it is taking me quite a while.

Thanks for any help or info!

Best,
S
 

jaduff46

macrumors 6502
Mar 3, 2010
328
187
Second star on the right....
Haven't used Linux, but usr/bin/sed (unix utility) will take care of replacing the spaces with underscores. Might work on the others too, but you may have to resort to the hex equivalents for the special characters.

CHOWN33 (forum regular) may have a better idea for this. Seems really knowledgeable in this domain.
 

ArtOfWarfare

macrumors G3
Nov 26, 2007
9,545
6,042
I wrote an obj-c based command line tool that does a bunch of character changing for me. I used obj-c instead of straight C because NSString's have a bunch of handy functions.

To do this, I needed these imports:

Code:
#include <stdio.h>
#include <Foundation/Foundation.h>

I got a path to a directory from the command line in main, and then I used it to make a file enumerator:

Code:
NSDirectoryEnumerator *fileEnumerator = [fileManager enumeratorAtPath:directory];
NSString *file;
while (file = [fileEnumerator nextObject])

Then just open the file with

Code:
NSString *contents = [NSString stringWithContentsOfFile:file usedEncoding:&encoding error:&error];

And then I used a line enumerator with a block (which is a built in,) but I could have used anything I wanted to manipulate the data... finally, I threw it back into the file using:

Code:
[newContents writeToFile:file atomically:YES encoding:encoding error:&error];

I compiled it, then threw the executable into... I forget where I put it, but it's now recognized system wide from the command line...

I hope that helps... I suppose it may not if you don't have any experience and you're not willing to become experienced with C/Obj-C.
 

ytk

macrumors 6502
Jul 8, 2010
252
5
I wrote an obj-c based command line tool that does a bunch of character changing for me

:eek: Good God, man! Go read up on sed and regexps before you hurt somebody with that thing!

I don't normally say this, but to any innocent readers who might happen on this: Do NOT do what ArtOfWarfare did. Ever. This is not just a case of reinventing the wheel—it's reinventing the internal combustion engine.

To AoW: I'm not trying to be insulting here, and I'm sure you did a fine job with your tool and it serves your needs adequately, but what you're describing is just fundamentally misguided in principle. There is simply no reason at all to re-implement a standard Unix tool that has been in use for thirty years to do something as simple as text processing.
 
Last edited:

chown33

Moderator
Staff member
Aug 9, 2009
10,706
8,346
A sea of green
The 'tr' command might be better than sed. It can trivially do the space-to-underbar translation.

It's slightly more difficult to tell 'tr' how to remove accents and other diacriticals. That would need the "equivalence class" feature. See the man page for details, and an example translating all accented forms of 'e' to an unaccented 'e'.


There are also Mac programs that can do batch renaming. One is called A Better Finder Rename. Another is called NameChanger (see macupdate.com). Yet another is called Renamer (at renamer.com). I found these with google search terms:
os x batch renamer

I used Better Finder Rename a long time ago, and it worked fine. I needed to do a more complex renaming than I was willing to write a 'tr' or 'sed' command for. I can't speak to any others, but they're easy to find.
 

travis.jennings

macrumors newbie
Jan 29, 2013
4
0
Perl regex

you can do this in perl, its trivial.

like from the command line:

perl -pi.bak -e 's/ /_/g' filename

that will edit in place the file of filename and change out spaces for underscores.

With a little bit of regex, you can do just about anything.
Like remove all chars which are not letters or numbers:

perl -pi.bak -e 's/[^A-Za-z0-9\.!@#$%^&*()]//g' filename

that would remove all chars NOT in [char list].

Perl is a great too for simple text extraction and modification. Of course you can write whole scripts and execute them, rather than command line stuff.
 

ArtOfWarfare

macrumors G3
Nov 26, 2007
9,545
6,042
:eek: Good God, man! Go read up on sed and regexps before you hurt somebody with that thing!

I don't normally say this, but to any innocent readers who might happen on this: Do NOT do what ArtOfWarfare did. Ever. This is not just a case of reinventing the wheel—it's reinventing the internal combustion engine.

I believe my needs were beyond what any built in tool could have done alone. I needed a way of quickly scanning hundreds of directories for files with a specific name, and then finding specific lines in the file and replacing specific characters in those lines with other characters. I may have been able to utilize something else, but I don't know enough about the different available scripting commands. Is there someplace I could learn about them all?
 

ytk

macrumors 6502
Jul 8, 2010
252
5
I believe my needs were beyond what any built in tool could have done alone. I needed a way of quickly scanning hundreds of directories for files with a specific name, and then finding specific lines in the file and replacing specific characters in those lines with other characters. I may have been able to utilize something else, but I don't know enough about the different available scripting commands. Is there someplace I could learn about them all?

Actually, that's a pretty simple task to accomplish with a single command line statement.

It sounds like a combination of find and sed would do the trick for you. The find command is a flexible and powerful way of finding files with any matching characteristic—name, type, creation date, etc. sed allows you to use regular expressions to search for matching content, and replace matching occurrences with text of your choosing. You could also use Perl instead of sed, as mentioned by travis.jennings, which probably gives you even more flexibility to do some things.

The most important thing for you to grasp in terms of pattern matching and replacement is regular expressions. There are plenty of tutorials around the web on regexps. They can be tricky to master, but once you are comfortable with them you'll be able to throw together a command to do exactly what you described in just a minute or two. Here's a good intro to find, grep, and sed, and I'm sure there are books that go into even greater depth on the Unix command line.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.