
dgilby
Novice
Dec 4, 2009, 4:22 AM
Post #14 of 14
(988 views)
|
|
Re: [dgilby] extracting a record from a file based on another file
[In reply to]
|
Can't Post
|
|
Hi Everyone How are things? Thanks for you input so far. Your guidance has produced this wonderful script. One problem though. I am still having trouble with GREP at the very end. Would anyone have any ideas on how to go about finishing this? Again I would like the data in the previous post to loop through the data and print the 20 records above and the 10 records after the timestamp. Any help would be greatly appreciated #!/usr/bin/perl -w use strict; use Tk; my $ImageFilePath; my $IMUFilePath; my $ResultFilePath; my $BeforeRecord = 20; my $AfterRecord = 10; my $mw = MainWindow -> new; $mw -> title("IMU Data Processing"); #my $types = [['Text Files','.txt'],['All Files','*'],]; # sizes window $mw -> geometry('400x200'); ######################### MENU BEGIN ############################### # menu my $mbar = $mw -> Menu(); $mw -> configure(-menu => $mbar); # Main Buttons my $file = $mbar -> cascade(-label => "File", -underline => 0, -tearoff => 0); my $edit = $mbar -> cascade(-label => "Edit", -underline => 0, -tearoff => 0); my $help = $mbar -> cascade(-label => "Help", -underline => 0, -tearoff => 0); #File Menu $file -> command(-label => "New", -underline =>0, -command => [\&menuClicked, "New"]); $file -> command(-label => "Open", -underline => 0, -command => [\&menuClicked, "Open"]); $file -> command(-label => "Save", -underline => 0, -command => [\&menuClicked, "Save As"]); $file -> separator(); $file -> command(-label => "Exit", -underline => 1, -command => sub {exit}); #Edit Menu #About/Help Menu $help -> command(-label => "Help", -underline => 0, -command => [\&menuClicked, "Help"]); $help -> separator(); $help -> command(-label => "About", -underline => 0, -command => [\&menuClicked, "About"]); ######################### MENU END ############################### ######################### GUI Begin ############################## #my $frm = $mw -> Frame(); # text explaining window my $lblInstructions = $mw->Label(-text => "This window processes IMU Data based on Aerial Camera Shutter Times", -padx=>5, -pady=>5); # label for Shutter times file my $lblImageFile = $mw->Label(-text => "Shutter File:", -padx=>5, -pady=>5); # entry textbox for Shutter times dialog my $entImageFile = $mw->Entry(-width=>30, -textvariable=>\$ImageFilePath); # button to browse for file my $butImageFile = $mw->Button(-text=>'Browse', -command=> sub { openImageFileDialog($entImageFile)}); # label for IMU dialog my $lblIMUFile = $mw->Label(-text => "IMU File:", -padx=>5, -pady=>5); # textbox for IMU times dialog my $entIMUFile = $mw->Entry(-width=>30, -textvariable=>\$IMUFilePath); # button to browse for file my $butIMUFile = $mw->Button(-text=>'Browse', -command=> sub { openIMUFileDialog($IMUFilePath)}); # label for output file my $lblResultFile = $mw->Label(-text => "Output File:", -padx=>5, -pady=>5); # textbox for output file dialog my $entResultFile = $mw->Entry(-width=>30, -textvariable=>\$ResultFilePath); # button to browse for file my $butResultFile = $mw->Button(-text=>'Browse', -command=> sub { saveResultFileDialog($ResultFilePath)}); # label for before records my $lblBeforeRecord = $mw->Label(-text => "Before Record", -padx=>5, -pady=>5); # textbox for before records my $entBeforeRecord = $mw->Entry(-width=>30, -textvariable=>\$BeforeRecord); # label for after records my $lblAfterRecord = $mw->Label(-text => "After Record", -padx=>5, -pady=>5); # textbox for after records my $entAfterRecord = $mw->Entry(-width=>30, -textvariable=>\$AfterRecord); # process file my $butProcessFile = $mw->Button(-text => "Process File", -width=>10, -command=>\&processFile); # Exit button my $butExit = $mw->Button(-text => "Exit", -width=>10, -command => sub { exit }); ######################### GUI End ################################ ######################### Geometry Management #################### #$frm -> grid(-row=>1); $lblInstructions -> grid(-row=>1, -column=>1, -columnspan=>4); $lblImageFile -> grid(-row=>2, -column=>1); $entImageFile -> grid(-row=>2, -column=>2); $butImageFile -> grid(-row=>2, -column=>3); $lblIMUFile -> grid(-row=>3, -column=>1); $entIMUFile -> grid(-row=>3, -column=>2); $butIMUFile -> grid(-row=>3, -column=>3); $lblResultFile -> grid(-row=>4, -column=>1); $entResultFile -> grid(-row=>4, -column=>2); $butResultFile -> grid(-row=>4, -column=>3); $lblBeforeRecord -> grid(-row=>5, -column=>1); $entBeforeRecord -> grid(-row=>5, -column=>2); $lblAfterRecord -> grid(-row=>6, -column=>1); $entAfterRecord -> grid(-row=>6, -column=>2); $butProcessFile -> grid(-row=>7, -column=>1, -columnspan=>2); $butExit -> grid(-row=>7, -column=>2, -columnspan=>2); ######################### Geometry End ########################### MainLoop; sub openImageFileDialog { my $imageFilePathOpen = $mw->getOpenFile(-title => 'Open Image File:'); $ImageFilePath = $imageFilePathOpen; } sub openIMUFileDialog { my $imuFilePathOpen = $mw->getOpenFile(-title => 'Open IMU File:'); $IMUFilePath = $imuFilePathOpen; } sub saveResultFileDialog { my $resultFilePathOpen = $mw->getSaveFile(-title => 'Save Result File:'); $ResultFilePath = $resultFilePathOpen; } sub processFile { # open image file for reading open IMAGEFILE, $ImageFilePath || die "Can't open $ImageFilePath: $!"; # open imu file for reading # open IMUFILE, $IMUFilePath || die "Can't open $IMUFilePath: $!"; # open result file for writing # open RESULTFILE, $ImageFilePath || die "Can't open $ImageFilePath: $!"; #while loop while (<IMAGEFILE>) { chomp; my @imagerecords = split (/\t/, $_); # prints records # print join ("\t", $imagerecords[0], $imagerecords[3], $imagerecords[10]). "\n"; # GNU Grep example #grep -A 20 -B 10 "<timestamp>" file # grep - searches for a file pattern # -A - # 20 - number records before timestamp # -B - # 10 - number records behind timestamp # "<timestamp>" - timestamp to look for # file - file to loop through grep -A $BeforeRecord -B $AfterRecord @imagerecords[3] $IMUFILE > RESULTFILE; } #while (<@imagerecords>) { # GNU Grep example #grep -A 20 -B 10 "<timestamp>" file # grep - searches for a file pattern # -A - # 20 - number records before timestamp # -B - # 10 - number records behind timestamp # "<timestamp>" - timestamp to look for # file - file to loop through # grep -A $BeforeRecord -B $AfterRecord @imagerecords[3] $IMUFILE > RESULTFILE; #}
|