Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Jan 28, 2013, 06:33 PM   #1
uzapuca
macrumors newbie
 
Join Date: Sep 2006
Batch Action / Remove Characters and Spaces

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
uzapuca is offline   0 Reply With Quote
Old Jan 29, 2013, 08:02 AM   #2
jaduff46
macrumors member
 
Join Date: Mar 2010
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.
jaduff46 is offline   0 Reply With Quote
Old Jan 29, 2013, 12:39 PM   #3
chown33
macrumors 603
 
Join Date: Aug 2009
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.
chown33 is offline   0 Reply With Quote
Old Jan 29, 2013, 01:59 PM   #4
travis.jennings
macrumors newbie
 
Join Date: Jan 2013
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.
travis.jennings is offline   0 Reply With Quote
Old Jan 29, 2013, 08:20 AM   #5
ArtOfWarfare
macrumors 603
 
ArtOfWarfare's Avatar
 
Join Date: Nov 2007
Send a message via Skype™ to ArtOfWarfare
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.
__________________
Don't tell me Macs don't last: 2007 iMac, 2007 Mac Mini, 2008 MacBook Air, all Vintage.
(iMac obsoletion: April 28, 2015, MBA: October 14, 2015, Mac Mini: March 9, 2016)
ArtOfWarfare is offline   0 Reply With Quote
Old Jan 29, 2013, 12:07 PM   #6
ytk
macrumors regular
 
Join Date: Jul 2010
Quote:
Originally Posted by ArtOfWarfare View Post
I wrote an obj-c based command line tool that does a bunch of character changing for me
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 by ytk; Jan 29, 2013 at 12:19 PM.
ytk is offline   1 Reply With Quote
Old Jan 29, 2013, 04:35 PM   #7
ArtOfWarfare
macrumors 603
 
ArtOfWarfare's Avatar
 
Join Date: Nov 2007
Send a message via Skype™ to ArtOfWarfare
Quote:
Originally Posted by ytk View Post
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?
ArtOfWarfare is offline   0 Reply With Quote
Old Jan 29, 2013, 05:24 PM   #8
ytk
macrumors regular
 
Join Date: Jul 2010
Quote:
Originally Posted by ArtOfWarfare View Post
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.
ytk is offline   1 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
iPhone: cant remove cydia profile no remove button alice11 Jailbreaks and iOS Hacks 10 Aug 29, 2014 08:27 PM
iPhone: Tweak to remove filter from photos in batch iamangeln Jailbreaks and iOS Hacks 0 May 29, 2014 03:11 AM
Characters zaze01 Mac OS X 10.7 Lion 2 Jan 31, 2013 09:29 PM

Forum Jump

All times are GMT -5. The time now is 01:39 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC