LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices



Reply
 
Search this Thread
Old 07-19-2005, 12:45 AM   #1
beebop
Member
 
Registered: Sep 2004
Location: Bend, Oregon
Distribution: Ubuntu Breezy and FC4
Posts: 43

Rep: Reputation: 15
using sed with parentheses in perl


I'm working on converting a bash script to perl. But for some reason, perl does not like my parentheses. Here's my perl code:

#!/usr/bin/perl
sub main
{
system "for i in `ls`; do mv \$i `echo \$i | sed '/mp3\$/ s#\(^[0-9]\{2\}\)\([0-9a-zA-Z]\+\)#\1-\2#'`; done"; # <-- this is a continuation of the line above
}
&main();

First a brief explanation of what it does: It goes through every file in the current directory (hence the "for i in ls") and renames that file. You can see the mv command buried in there, the first argument being the original file name ($i) and the second arg being the renamed file. A sample file would be 01Artist-Song.mp3. After running the script, the file would be 01-Artist-Song.mp3 where the difference is the added dash between 01 and Artist.

The problem is in the sed command. In Bash, the first parentheses group is named 1 (which will be the first two digits), and the second is named 2 (which is the rest of the filename, not including the .mp3). I reuse those groups in the second part of sed, but in my perl script, the parentheses are not recognized. sed skips the replacement leaving the filename the same, and mv tries to move the old filename to the same filename resulting in an error for each file:

mv: `01PaulSimon-TheBoyInTheBubble.mp3' and `01PaulSimon-TheBoyInTheBubble.mp3' are the same file.

I have a lot of bash scripts I'd like to convert to perl and the 'system' perl command seemed like an easy fix. Maybe not....
 
Old 07-19-2005, 03:16 AM   #2
Harmaa Kettu
Member
 
Registered: Apr 2005
Location: Finland
Posts: 196

Rep: Reputation: 30
Quote:
I have a lot of bash scripts I'd like to convert to perl and the 'system' perl command seemed like an easy fix. Maybe not....
What is the point of converting them if you only run shell commands from the converted versions?

Here is a better solution for this one:
Code:
foreach my $file (glob "*.mp3")
{
  if($file =~ /^(\d+)([^\d-].*)/)
  {
    rename $file, "$1-$2";
  }
}
 
Old 07-21-2005, 11:41 PM   #3
beebop
Member
 
Registered: Sep 2004
Location: Bend, Oregon
Distribution: Ubuntu Breezy and FC4
Posts: 43

Original Poster
Rep: Reputation: 15
<quote>What is the point of converting them if you only run shell commands from the converted versions?</quote>

Hehe, good question. I really like the option feature in perl. I'm including -h tags so I can actually remember what the hell my scripts mean, as well a a quick usage reference.

I guess I ~could~ rewrite all my scripts in perl. Probably easier in the long run...

Thanks for the code. It's a lot easier to read! This

if($file =~ /^(\d+)([^\d-].*)/)

is new to me. =~ is a "string equal" or something? Why not use ==? or -e?
 
  


Reply


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 On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help on sed and perl needed. cash_05 Programming 7 09-03-2005 05:33 PM
SED, AWK or PERL HELP embsupafly Programming 6 08-20-2005 10:07 PM
sed error: "parentheses not balanced" - But they are! Travis86 Programming 2 08-12-2004 08:08 PM
SED + perl !!! letmein Linux - General 9 10-07-2003 06:10 PM
perl with sed !!! letmein Linux - General 1 08-18-2003 06:26 PM


All times are GMT -5. The time now is 08:24 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration