LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   file renaming question--replace multiple instances (https://www.linuxquestions.org/questions/linux-general-1/file-renaming-question-replace-multiple-instances-610251/)

David the H. 12-31-2007 05:22 PM

file renaming question--replace multiple instances
 
I've been trying to use the rename command to remove certain non-standard characters from filenames, but I can't get it to work the way I want.

Now the rename command uses pretty much the same syntax as sed. Trouble is, I don't know sed well enough to make this work.

I can usually use the 'y/a/b/' substitution syntax to replace all instances of a character with another. But let's say, for example, that I want to simply remove all instances of commas from a filename, say "Beatles--long,_long,_long.ogg". If I try something like:

rename 'y/,//'

it completely fails to work. It appears to be because the substitution field is blank; it needs something to replace the comma with. But I don't want it to be replaced with anything, or rather, I want it to be replaced with nothing. I just want the character removed from the file name. I can run

rename 's/,//'

with no problem, but that only replaces the first instance of the quote in the name, so I have to run it multiple times to be sure I got everything. Even worse is something like

rename 'y/[,\']//'

where I want to remove both commas and apostrophe marks (Beatles--Honey,_Don't.ogg).

So my question is, what syntax do I have to use to remove multiple instances of a character? My eventual goal is to to create a simple 'removefilenamejunk' alias or command that I can run whenever I want to clean up a file name. Thanx.

David1357 12-31-2007 09:27 PM

Quote:

Originally Posted by David the H. (Post 3007048)
rename 's/,//'

Try rename 's/,//g', the "g" is for global and will replace all instances on the line.

ghostdog74 12-31-2007 09:41 PM

Quote:

Originally Posted by David the H. (Post 3007048)
Now the rename command uses pretty much the same syntax as sed. Trouble is, I don't know sed well enough to make this work.

Code:

echo $string | sed 's/,//g'
"g" for global replacement

David the H. 12-31-2007 11:51 PM

I just knew it would be something simple like that. I swear though that I'd tried the g flag sometime in my searching and had no luck. Must've done something wrong. Besides, the sed man page only gives g as:

g G Copy/append hold space to pattern space.

which doesn't sound anything like "global" replacement to me. I'm not even sure what "hold space" and "pattern space" mean, exactly.

Anyway, I learned something new. Thanks!

ghostdog74 01-01-2008 12:05 AM

Quote:

Originally Posted by David the H. (Post 3007257)
Besides, the sed man page only gives g as:

those you are looking at are for address ranges. For substitution, its called flags. See here ( and read the whole book if you have time )


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