LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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 12-20-2010, 09:43 AM   #1
npeddle
LQ Newbie
 
Registered: Dec 2010
Posts: 4

Rep: Reputation: 0
Renaming Files..Help


I need help with wrinting a script that renames multiple files to the following format output.YYYYMMDDHHMM where the date is found inside the files as shown
05-14-2010,06:30:01,
All help is appreciated.

Thanks
 
Old 12-20-2010, 09:55 AM   #2
TB0ne
Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 15,102

Rep: Reputation: 2719Reputation: 2719Reputation: 2719Reputation: 2719Reputation: 2719Reputation: 2719Reputation: 2719Reputation: 2719Reputation: 2719Reputation: 2719Reputation: 2719
Quote:
Originally Posted by npeddle View Post
I need help with wrinting a script that renames multiple files to the following format output.YYYYMMDDHHMM where the date is found inside the files as shown
05-14-2010,06:30:01,
All help is appreciated.
Thanks
Ok, what have you written so far?

Check the man pages for the "cut" command, and check into sed. This is very quick and dirty, but may get you a starting point:
Code:
ls *.<extension> | sed 's/\-//g' | sed 's/\,//g' | sed 's/\://g' | cut -c 5-8
Will return you "2010", the year. Repeat with other variables for Month, Day, and time, and you'll be left with your file name. There are thousands of bash scripting tutorials on the web you can find, with examples on how to read a list of files from a directory, and do things with that list.
 
Old 12-20-2010, 10:06 AM   #3
npeddle
LQ Newbie
 
Registered: Dec 2010
Posts: 4

Original Poster
Rep: Reputation: 0
This is what I have and it renames the file fine but I am only getting the last line of the file when it has been renamed. Do you know why?


if ($ARGV[0] eq "") {
print "Must supply a raw data file and a site directory\n"; exit
} elsif ($ARGV[1] eq "") {
print "Must also supply a site directory\n"; exit
} else {
chdir ($ARGV[1]) || die "Can not cd to $ARGV[1]"
};
$out_fil=$ARGV[0] . ".out";
$filt=`sed "s/ / /g" $ARGV[0] > $out_fil`;
open (RAW, $out_fil) || die "Can not open Raw Data for reading";
while (<RAW>) {
$test0=substr($_,0,1);
if ($_ =~ /DATE=/) {
$year=substr($_,6,4);
$mon=substr($_,0,2);
$day=substr($_,3,2);
$hr=substr($_,11,2);
$min=substr($_,14,2);
if ($mon eq "01") {
$year=$year-1;
}
};
if ($test0 eq "0" || $test0 eq "1") {
$year=substr($_,6,4);
$d_mon=substr($_,0,2);
$d_day=substr($_,3,2);
$d_hr=substr($_,11,2);
$d_min=substr($_,14,2);
$fil_name="passive." . $year . $d_mon . $d_day . $d_hr . $d_min;
open (NEW_DAT, ">$fil_name") || die "Can not create passive file $fil_name";
print NEW_DAT"";
print "$out_fil"
close ($out_fil);
} else {
open(NEW_DAT, ">>$fil_name") ||die "Can not open output file $1";
print NEW_DAT"";
print "$out_fil"
close($out_fil);
};
};

close (RAW);
unlink ("$out_fil");
 
Old 12-20-2010, 10:12 AM   #4
npeddle
LQ Newbie
 
Registered: Dec 2010
Posts: 4

Original Poster
Rep: Reputation: 0
Sorry , this is actually what I have but only getting one line of data in the new file


Code:
if ($ARGV[0] eq "") {
        print "Must supply a raw data file and a site directory\n"; exit
} elsif ($ARGV[1] eq "") {
        print "Must also supply a site directory\n"; exit
} else {
        chdir ($ARGV[1]) || die "Can not cd to $ARGV[1]"
};
$out_fil=$ARGV[0] . ".out";
$filt=`sed "s/  / /g" $ARGV[0] > $out_fil`;
open (RAW, $out_fil) || die "Can not open Raw Data for reading";
while (<RAW>) {
        $test0=substr($_,0,1);
        if ($_ =~ /DATE=/) {
                $year=substr($_,6,4);
                $mon=substr($_,0,2);
                $day=substr($_,3,2);
                $hr=substr($_,11,2);
                $min=substr($_,14,2);
                if ($mon eq "01") {
                        $year=$year-1;
                }
        };
        if ($test0 eq "0" || $test0 eq "1") {
                $year=substr($_,6,4);
                $d_mon=substr($_,0,2);
                $d_day=substr($_,3,2);
                $d_hr=substr($_,11,2);
                $d_min=substr($_,14,2);
                $new_line=substr($_,0,400);
                @val=split(/ /,$new_line);
                $i=1;
                foreach $d_val (@val) {
                        if ($d_val ne "" && $d_val ne " " && $i < 24) {
                                if ($i < 10) {
                                        $hr="0" . $i . "00";
                                } elsif ($i == 24) {
                                        $hr="0000";
                                } else {
                                        $hr=$i . "00";
                                }
                                $fil_name="passive." . $year . $d_mon . $d_day . $d_hr . $d_min;
                                open (NEW_DAT, ">$fil_name") || die "Can not create passive file $fil_name";
                                print NEW_DAT "";
                                print NEW_DAT "$d_val\n";
                                close (NEW_DAT);
                                $i++;
                        };
                };
        }; 
        
};
close (RAW);
unlink ("$out_fil");

Last edited by Tinkster; 12-20-2010 at 11:34 AM. Reason: added code tags
 
Old 12-20-2010, 10:26 AM   #5
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,698

Rep: Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988
Well I am not so much a Perl guru, but once you have the date in the format you have show (05-14-2010,06:30:01) you could run it through something like:
Code:
echo '05-14-2010,06:30:01' | awk -F"[-,:]" '{print $3 $1 $2 $4 $5}'
 
Old 12-20-2010, 11:12 AM   #6
TB0ne
Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 15,102

Rep: Reputation: 2719Reputation: 2719Reputation: 2719Reputation: 2719Reputation: 2719Reputation: 2719Reputation: 2719Reputation: 2719Reputation: 2719Reputation: 2719Reputation: 2719
Quote:
Originally Posted by npeddle View Post
Sorry , this is actually what I have but only getting one line of data in the new file
I think you're over-complicating things. Instead of reading the file contents, and outputting it to a new file, why not just run a "mv" command??? You know the input file name, and have manipulated it to be the format you want. So just run "mv <input file varialbe name> <output file variable name>"
 
Old 12-20-2010, 11:35 AM   #7
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,005
Blog Entries: 11

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
Quote:
Originally Posted by TB0ne View Post
I think you're over-complicating things. Instead of reading the file contents, and outputting it to a new file, why not just run a "mv" command??? You know the input file name, and have manipulated it to be the format you want. So just run "mv <input file varialbe name> <output file variable name>"
The date stamp comes from inside the file... so reading is obviously
of the essence. I'm not sure whether he really meant to read from
the output file.


OP, welcome to LQ!

Can you please describe the entire process a bit more clearly?
Maybe I'm missing the point, but as TB0ne pointed out: once
you have your time-stamp, and are ready to copy/move the file
there's really no need to read/write the file line by line.
Code:
use File::Copy;
copy(source,target);

Cheers,
Tink

Last edited by Tinkster; 12-20-2010 at 11:38 AM.
 
Old 12-20-2010, 12:39 PM   #8
TB0ne
Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 15,102

Rep: Reputation: 2719Reputation: 2719Reputation: 2719Reputation: 2719Reputation: 2719Reputation: 2719Reputation: 2719Reputation: 2719Reputation: 2719Reputation: 2719Reputation: 2719
Quote:
Originally Posted by Tinkster View Post
The date stamp comes from inside the file... so reading is obviously
of the essence. I'm not sure whether he really meant to read from
the output file.
You are quite right....missed that in the first post. OP, my apologies for making the waters muddy due to inattention.
Quote:
OP, welcome to LQ!
Can you please describe the entire process a bit more clearly?
Maybe I'm missing the point, but as TB0ne pointed out: once
you have your time-stamp, and are ready to copy/move the file
there's really no need to read/write the file line by line.
Code:
use File::Copy;
copy(source,target);
Indeed...since the OP will have the file name (got to reference it to OPEN the file to start with), and the date info, the tools are there.
 
Old 12-20-2010, 02:30 PM   #9
npeddle
LQ Newbie
 
Registered: Dec 2010
Posts: 4

Original Poster
Rep: Reputation: 0
Thanks for trying to help but I am still not getting it.
 
Old 12-20-2010, 03:17 PM   #10
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,005
Blog Entries: 11

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
Quote:
Originally Posted by npeddle View Post
Thanks for trying to help but I am still not getting it.

Which part are you not getting?


Cheers,
Tink
 
  


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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with renaming files EchoPhyber Linux - Newbie 4 03-26-2010 09:38 AM
Help renaming files nandelbosc Programming 2 11-26-2009 08:24 AM
Renaming files mashcaster Linux - General 3 05-08-2009 04:14 PM
Renaming files Viablade Linux - Newbie 11 10-10-2008 02:54 PM
renaming files TomalakBORG Linux - Newbie 4 12-24-2005 11:14 AM


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