LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-29-2011, 10:18 AM   #1
ezekieldas
Member
 
Registered: Mar 2010
Posts: 122

Rep: Reputation: 16
perl: split filename.jpg into $prefix.$ext


Howdy. Confused about this one. I have this loop that matches certain extensions but I can't seem to get the filename prefix out of it. Any suggestions are appreciated.

PHP Code:
#!/usr/bin/perl

my $dir ".";
opendirDIR$dir );
my @files readdirDIR );
closedirDIR );

sub get_rand_string {
 
my @chars=('a'..'z''A' .. 'Z''0' .. '9');
 
$rand_string join("", @charsmap rand @chars } ( .. ) ]);
 return 
$rand_string;
}

foreach 
my $file( @files ) {
    if ( 
$file =~ /\w+\.(jpg|JPG|gif)$/ ) {

#       how can I grab the prefix here? I want to split
#       the filename into $prefix.$ext 
#
#       As seen below I can get the $1 from the if statement
#       above.
#
#    I've tried the following... 
#
#       $orig_prefix = s/\.[^.]*$//;
#       $orig_prefix =~ s/\.\w+$//;
#       print "[$orig_prefix]\n";
#       $orig_prefix .= FOO;

        
my $ext = $1;

        
my $rand_string=&get_rand_string();

#     So ideally I'd like to place the prefix back on the filename 
#    with just the random string inserted... 

        
my $filename_rand sprintf"$orig_prefix_$rand_string.$ext);
#        my $filename_rand = sprintf( "$rand_string.$ext" );

        
system"mv $file $filename_rand);
    }

 
Old 09-29-2011, 06:21 PM   #2
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,790

Rep: Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653
Been a little while since I've done perl but basically changing it like so -

Code:
if ( $file =~ /(w+).(jpg|JPG|gif)$/ ) {
..will give you 2 back references - $1 and $2, $1 will be the filename and $2 will be the extension
 
1 members found this post helpful.
Old 09-29-2011, 06:40 PM   #3
ezekieldas
Member
 
Registered: Mar 2010
Posts: 122

Original Poster
Rep: Reputation: 16
Hey --thanks a bunch kbp! I still need to anchor (?) the w+ though:

if ( $file =~ /(\w+)\.(jpg|JPG|gif)$/ ) {



Just so I understand this better --do you, or does anyone, know what this is called technically? I iterate through elements of an array and if they match they're assigned $1, $2, etc? How would you communicate that in proper perl-speak?

Last edited by ezekieldas; 09-29-2011 at 06:55 PM.
 
Old 09-29-2011, 07:58 PM   #4
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,790

Rep: Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653
The matching parts that are surrounded by '()' are called backreferences. You don't need to anchor the front unless you want to as you're specifically only matching words (\w).

They're called back references as you commonly use them in search/replace like:

Code:
's/(first_bit_I_want).*(second_bit_I_want).*(third_bit_I_want)/$1:$3:$2/'
From the example you can see that you can swap the ordering of the matching parts, add delimiters etc

Last edited by kbp; 09-29-2011 at 08:01 PM. Reason: added example
 
Old 09-30-2011, 11:14 AM   #5
anomie
Senior Member
 
Registered: Nov 2004
Location: Texas
Distribution: RHEL, Scientific Linux, Debian, Fedora
Posts: 3,935
Blog Entries: 5

Rep: Reputation: Disabled
Also, another approach with no regex. Use split():
Code:
my $filename = 'perky.jpg' ;

my @tokens = split(/\./, $filename) ;

print shift(@tokens) . "\n" ;
 
Old 10-03-2011, 05:09 PM   #6
ezekieldas
Member
 
Registered: Mar 2010
Posts: 122

Original Poster
Rep: Reputation: 16
Thanks again to all the replies here -- very helpful!
 
  


Reply



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
split filename Cerephim Linux - Server 8 01-20-2011 10:54 AM
split a gzip file based on size and have a prefix without changing extension gubbu Linux - Newbie 3 01-18-2010 05:09 PM
Adding text(prefix) to a filename IkerSamu Programming 2 04-10-2007 06:47 AM
Split CSV, field as filename richmur Programming 2 10-24-2006 08:39 AM
controling ext. devices with perl DanTaylor Programming 1 01-30-2006 01:20 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:56 PM.

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