LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Oneline filename batch rename (specific sed instance pattern matching) (https://www.linuxquestions.org/questions/linux-newbie-8/oneline-filename-batch-rename-specific-sed-instance-pattern-matching-843767/)

briana.paige 11-11-2010 05:36 PM

Oneline filename batch rename (specific sed instance pattern matching)
 
Hi,

I apologise for creating yet another batch rename thread, I know that there is a lot of information on it and I have attempted for a couple hours to find a solution using google and other threads. If I missed a suitable answer I apologise ahead of time.

I would like to use only one liners from my bash command window.

I have a bunch of files that were named with periods as separators. I used the following command to remove the periods and tweak a few other things.

Code:

rename . _ *.avi
Original filename:
Code:

get.smart.1965.s01e01.Mr.Big.dvdrip.xvid-fqm.avi
Current filename:
Code:

Get_Smart_(1965)_01e01_Mr_Big.avi
Desired filename:
Code:

Get Smart (1965) 01e01 - Mr Big.avi
I have been trying to use this command so that I can replace the fourth instance of '_' with '-' but a lot of commands give me errors while they seem to be working for other people. I have bash, sed, awk, gawk and perl installed and am using Cygwin on Windows XP. I cannot use simple matching as I did previously because the number preceding the dash will be different for each file.

Code:

ls *.avi | awk '{print("mv "$1" "$1)}' | sed 's/\_/\-/4'|/bin/sh
This previews what the command will do if you remove the |/bin/sh and the preview shows the correct operation. However when I append the |/bin/sh to actually execute it, I get this error:
Code:

syntax error near unexpected token `('
Finally, along the same lines, when I am ready to remove the '_' and replace it with spaces, I can use the command
Code:

rename \_ \ *.avi
as I did to make the first set of changes above. I noticed that this command seems to only change the first instance and I have to run it multiple times to remove all of them. How can I make this apply to all pattern instances?

Thanks in advance.

briana.paige 11-11-2010 05:56 PM

Brute force
 
I managed to continue using these commands to do the batch rename:

Code:

rename \_ \-\ *.avi
rename \_ \ -\  *.avi

I manually replaced the '.' with '-' after I ran this command 3 times to change the fourth instance.

Code:

rename \_ \  *.avi
Original sample filename:
Code:

get.smart.1965.01e02.Diplomats.Daughter.Diplomats.Daughter.Diplomats.Daughter.avi
Final sample filename using variations of above commands:
Code:

Get Smart (1965) 01e02 - Diplomats Daughter Diplomats Daughter Diplomats Daughter.avi
I would still like to know if there is a more elegant form for this though so that I could just do a global search and replace of all '.' to '_' for instance and then say change the fourth instance of '_' to '-'.

Thanks.


All times are GMT -5. The time now is 02:13 AM.