ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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?
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.