#!/usr/bin/perl -w # --------------------------------------------------------- # # Name: start_end.plx # # Read a list of file names from a file called herrping_filelist.txt. # For each file name, open the file, get the first row of data (omit the header line). # Find the last line of data, get the time, lat and lon and # append these 3 items to the end of the first line in an output file. # Get the next file listed in herrping_filelist.txt and repeat, # appending each line to the output file as a new line. # --------------------------------------------------------- # Read a list of file names from a file called herrping_filelist.txt. my $listfile = "herrping_filelist.txt"; #this is the name of the file with a list of filenames my $outfile = "outfile.dat"; #print STDOUT ("output file $outfile created.\n"); #create output file: open OUT, "> $outfile" or die "Can't open $outfile.\n"; # For each file name, open the file # read each line in herrping_filelist.txt (each line is a file name), open that file, read a line and parse it. open LIST, "<$listfile" or die "Can't open $listfile.\n"; @fnames = ; print STDOUT ("fnames: @fnames\n"); close LIST; print OUT "ping year month_local day_local time_local_start lat-start lon_start time_local_end lat_end lon_end\n"; foreach $fname (@fnames){ chomp($fname); print STDOUT ("fname_string: $fname\n"); #current filename my @data; # For each file name, open the file, read a line and parse it. open IN, "< $fname" or die "Can't open data file $fname.\n"; foreach $line (){ chomp($line); #removes \n from end of line $line=~s/\r\n//; # @fields = split(" ",$line); #separate on spaces #assign names to each variable push @data,$line; # my ($ping,$year,$month_local,$day_local,$time_local,$lat,$lon)=@fields; # ($ping,$year,$month_local,$day_local,$time_local,$lat,$lon)=%line; } #print STDOUT ("ping $ping, year $year, month $month_local, day $day_local\n"); #print STDOUT ("fields element 1-6: @fields[0,1,2,3,4,5,6]\n"); my $new_line; my $last_line=$data[$#data]; my @last_line=split(" ",$last_line); my $first_line=$data[1]; $new_line=$first_line.join("\t",($last_line[4],$last_line[5],$last_line[6])); print OUT $new_line,"\n"; #print OUT ("@fields[0-6]"); # find line 2 (use find $ping is a number, or first line that starts with numbers) # if ($ping =~ /\d+/) { # if $ping =~ /(\d+)/; # if ($ping =~/[0-9]+/); #write line 2 to the outfile.dat # open OUT,">> $outfile." or die "Can't open output file.\n"; # print OUT "@line"; # find last line in filename(i) # find lat and lon and append them to the previous line # if (eof()) { # check for end of current file # print OUT ("\t$time_local\t$lat\t$lon\n"); # print STDOUT ("time and position written to file.\n"); # } } close IN; close OUT;