Review your favorite Linux distribution.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 11-11-2010, 06:36 PM   #1
LQ Newbie
Registered: May 2009
Location: Canada
Posts: 14

Rep: Reputation: 0
Oneline filename batch rename (specific sed instance pattern matching)


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.

rename . _ *.avi
Original filename:
Current filename:
Desired filename:
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.

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:
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
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.
Old 11-11-2010, 06:56 PM   #2
LQ Newbie
Registered: May 2009
Location: Canada
Posts: 14

Original Poster
Rep: Reputation: 0
Brute force

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

rename \_ \-\ *.avi
rename \_ \ -\  *.avi
I manually replaced the '.' with '-' after I ran this command 3 times to change the fourth instance.

rename \_ \  *.avi
Original sample filename:
Final sample filename using variations of above commands:
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 '-'.



batch, filenames

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Pattern matching with sed vinaytp Linux - Newbie 3 05-24-2010 08:33 AM
SED multiline pattern matching AutoC Programming 1 07-18-2009 01:04 AM
sed: pattern matching with newlines anjanesh Linux - General 2 02-20-2009 07:36 AM
Sed pattern matching digitalbrutus Programming 1 08-20-2006 02:37 PM
pattern matching problem in sed digitalbrutus Programming 4 08-20-2006 05:40 AM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:24 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration