Hi Guys,
I have a file a.txt
Start Date/Time End Date/Time from Prob_Dura.
----------------- ----------------- ----- ------
20090525 23:58:59 20090526 00:00:00 machine1 000051
20090525 23:58:09 20090526 00:00:11 machine2 000150
The perl or shell script can:
- remove first two lins.
- remove column "from".
- calculate how many seconds between End and Start Date/Time.
- calculate how many seconds for the Prob_Dura, now the format is "hh/mm/ss".
The output should be:
61 51
122 110
I read the thread here, and know awk, sed can do, but when trying on my file, then still cannot figure out... ---
Thanks
put here your awk, even does not work. Let see it together.
Start Date/Time End Date/Time from Prob_Dura
----------------- ----------------- ----- ------
20090525 23:58:59 20090526 00:00:00 machine1 000051
20090525 23:58:09 20090526 00:00:11 machine22 000150
-
remove the first two lines.
awk 'FNR>2' a.txt
-
remove the columun for from
cut -f1,2,3,4,6 -d' ' a.txt
but i fail to do that, becuase you can see the line no.2 machines22 + one space + 000150; but line no.1 is two spaces between the "from" and "Prob_Dura". So the -f6 is the empty character.
i stuck here.
Please advice, any other good way can select what i want, or solve the more than one space issue.
Thanks
Hi, if your day range is less than one month, i think below perl script is ok.
But if you day range may be more than one month, need to take diversity days-per-month into account and modify below code accordingly, which is a little bit complex:)
sub toSecond{
my ($time)=@_;
my @tmp=split(":",$time);
return $tmp[0]*3600+$tmp[1]*60+$tmp[2];
}
while(<DATA>){
next if $. < 3;
chomp;
my @tmp=split;
$tmp[5]=~s/([0-9]{2})([0-9]{2})([0-9]{2})/$1*3600+$2*60+$3/e;
print toSecond($tmp[3])-toSecond($tmp[1])+ ($tmp[2]-$tmp[0])*24*60*60," ",$tmp[5],"\n";
}
__DATA__
Start Date/Time End Date/Time from Prob_Dura.
----------------- ----------------- ----- ------
20090525 23:58:59 20090526 00:00:00 machine1 000051
20090525 23:58:09 20090526 00:00:11 machine2 000150
Hi cherry,
It works perfectly. I always thought awk, sed, those commands, but yours coding is so simple.
instead of
__DATA__
Start Date/Time End Date/Time from Prob_Dura.
----------------- ----------------- ----- ------
20090525 23:58:59 20090526 00:00:00 machine1 000051
20090525 23:58:09 20090526 00:00:11 machine2 000150
how i can do with a file, i mean the start date... 2009... 000051, those data in a "a.txt". how the perl script can read from a file, stead of __DATA__?
Thanks
-----Post Update-----
$data_file="a.txt";
open(DAT, $data_file) || die("Could not open file!");
while(<DAT>){