LinuxQuestions.org
Help answer threads with 0 replies.
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 12-03-2007, 11:52 PM   #1
saltydog4791
LQ Newbie
 
Registered: Dec 2007
Posts: 2

Rep: Reputation: 0
perl matching script


Hello all,

I am new to all of this so be gentle.

I am working on a script that will match 2 files that will always have the same naming scheme and cat one to the other.

Basically I will have files like

102907-cf.txt
103007-cf.txt
103107-cf.txt
110107-cf.txt

and

TWUS 24K 102907-cf.txt
TWUS 24K 103007-cf.txt
TWUS 24K 103107-cf.txt
TWUS 24K 110107-cf.txt

I need to cat the TWUS files to the matching other file based on the month and day. So TWUS 24K 102907-cf.txt would be cat'd to the end of 102907-cf.txt, TWUS 24K 110107-cf.txt would be cat'd to end of 110107-cf.txt, etc.

After the cat'ing is done I have a piece of perl code that "pretties" the file name into a file like "November 01, 2007". That code is this:


Code:
 
#!/usr/bin/perl 
 
#use strict; 
use warnings; 
 
my %mons = ("01", January, "02", February, "03", March, "04", April, "05", May, "06", June, "07", July, "08", August, "09", September, "10", October, "11", November, "12", December); 
 
 
foreach my $filename (@ARGV) 
{ 
    if ($filename =~ /^(\d\d)(\d\d)(\d\d)\-cf.txt$/) 
    { 
        my $month = $1;  
        my $day = $2;  
        my $year = $3;  
		 
        $year += 2000; 
        my $newFilename = "$mons{$month} $day, $year.txt";  
 
        system "mv $filename '$newFilename'"; 
    } 
}


I would love to some how merge this into one perl script. Right now I have two separate folders for each type of file with some bash script that does the cat with a wildcard that can only work if there is only one file in each directory. This clearly is not the right way to do it. I need a way to process multiple files based on matching and then doing the renaming all in the same perl script.

I know it's possible but I am stumped and would love some assistance from the community. Thank you so much.
 
Old 12-04-2007, 12:45 AM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
not a perl script,
Code:
awk 'BEGIN{
    #FS="-"
    month["01"]="January"
    month["02"]="February"
    month["03"]="March"
    month["04"]="April"
    month["05"]="May"
    month["06"]="June"
    month["07"]="July"
    month["08"]="August"
    month["09"]="September"
    month["10"]="October"
    month["11"]="November"
    month["12"]="December"
}
{

    n=split(FILENAME,f,"-")
    m=split(f[1],name," ")
    filename = name[3]"-cf.txt"
    cmd="test -f " filename
    ret = system(cmd)
    if ( ret == 0 ) {
       print $0 >> filename
    }
    m = substr(name[3],0,2)
    d = substr(name[3],3,2)
    y = substr(name[3],5,2)
    newfile = month[m]" "d","y    
    cmd = "mv \047" FILENAME "\047 \047" newfile"\047"
    print cmd
    #system(cmd) #uncomment to rename file.
    nextfile
}
' TWUS*
 
Old 12-04-2007, 04:47 PM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
The system cmd should be, if I understand the requirement

system(cat 'TWUS 24K '.$filename >> $newFilename");

Can i just say that's a horrible format for a filename btw. Most Unix tools will have problems with spaces in the filename unless you take special precautions. Not homework by any chance?
 
Old 12-05-2007, 02:01 PM   #4
Quigi
Member
 
Registered: Mar 2003
Location: Cambridge, MA, USA
Distribution: Ubuntu (Dapper and Heron)
Posts: 377

Rep: Reputation: 31
Agree with chrism01 about spaces, and the system() call for concatenating (his misses the opening double quote.) If you choose a pretty name like "2007-11-01.txt", alphabetical order will also be chronological. For renaming in Perl, you don't need the clunky "system" hammer. Taking this together, the body of your loop becomes
Code:
    if ($filename =~ /^(\d\d)(\d\d)(\d\d)-cf.txt$/) 
    { 
        my $newFilename = "20$3-$1-$2.txt";  
        rename $filename, $newFilename or die $!;
        system("cat 'TWUS 24K '.$filename >> $newFilename");
    }
Take the parts you like :-) Maybe add error checking to the system() call.
 
Old 12-05-2007, 04:52 PM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Y, missing double quote was a typo. That's why I always use -w and use strict...
 
  


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
Embedded regex matching in Perl GATTACA Programming 5 01-17-2007 09:16 AM
Perl global matching ShaqDiesel Programming 1 11-07-2006 08:54 PM
Perl pattern matching in VB rigel_kent Programming 1 05-30-2006 11:00 AM
perl regex matching exodist Programming 2 11-15-2004 10:50 PM
pattern matching in perl ludeKing Programming 9 04-02-2004 09:53 AM

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

All times are GMT -5. The time now is 02:44 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