Linux - NewbieThis 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
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 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.
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.
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);
};
};
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:
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>"
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.