LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices


Reply
  Search this Thread
Old 11-11-2010, 05:36 PM   #1
briana.paige
LQ Newbie
 
Registered: May 2009
Location: Canada
Posts: 14

Rep: Reputation: 0
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.
 
Old 11-11-2010, 05:56 PM   #2
briana.paige
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:

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.
 
  


Reply

Tags
batch, filenames



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 07:33 AM
SED multiline pattern matching AutoC Programming 1 07-18-2009 12:04 AM
sed: pattern matching with newlines anjanesh Linux - General 2 02-20-2009 06:36 AM
Sed pattern matching digitalbrutus Programming 1 08-20-2006 01:37 PM
pattern matching problem in sed digitalbrutus Programming 4 08-20-2006 04:40 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:40 AM.

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