Hello,

I am new to perl and I would like to parse a csv file in perl. The current code I have opens a file and prints new output to the terminal. I'm wondering how to write to print this information into the file instead of to the terminal. I tried to use

open (FILE, ">>gps_sample.txt") but it wouldn't print to the file. Below is my current code.

#!/usr/bin/perl

#use warnings;
#use strict;

my $string_type;
my $timestamp;
my $validity;
my $lat;
my $ns;
my $lon;
my $ew;
my $speed;
my $true;
my $date;
my $variation;
my $ew2;
my $check;

open (FILE, 'gps_sample.txt');
while (<FILE>) {
chomp;
($string_type, $timestamp, $validity, $lat, $ns, $lon, $ew, $speed, $true, $date, $variation, $ew2, $check) = split(",");
print "String Type: $string_type\n";
#print FILE "String Type: $string_type\n";
print "Time: $time\n";
print "Validity: $validity\n";
print "Latitude: $lat\n";
print "North/South: $ns\n";
print "Longitude: $lon\n";
print "East/West: $ew\n";
print "Speed: $speed\n";
print "True Course: $true\n";
print "Date: $date\n";
print "Variation: $variation\n";
print "East/West: $ew2\n";
print "Check: $check\n";

print "---------\n";
}
close (FILE);

I wouldn't want to try reading sequentially from a file and appending to it within the same loop. Instead I would read from one file and print output into a new file opened for output. After you have read through your input file and written everything you want to the output file, close both files.

After doing the above, you can easily append the new file onto the end of your original file.

I wouldn't want to try reading sequentially from a file and appending to it within the same loop. Instead I would read from one file and print output into a new file opened for output. After you have read through your input file and written everything you want to the output file, close both files.

After doing the above, you can easily append the new file onto the end of your original file.

Thanks for the suggestion. I have read from one file and just wrote to another file. Now I have an issue with linefeeds/newlines/carraige returns. The data that I want to parse is the data that I attached. I need to remove the new lines/carriage/returns/linefeeds so that my output won't print out multiple blank information. here is my code:

#!/usr/bin/perl

#use warnings;
#use strict;

my $string_type;
my $timestamp;
my $validity;
my $lat;
my $ns;
my $lon;
my $ew;
my $speed;
my $true;
my $date;
my $variation;
my $ew2;
my $check;
my $line;


#my $infile = 'gps_sample.txt';
#my $outfile = 'gps_sample2.txt';


open (FILE, '2010-01-20-17:23-gps-data');
open (FILE2, ">revgps");

#my $line =~ s/(\n|\r)//g;

while (<FILE>) {
chomp;
($string_type, $timestamp, $validity, $lat, $ns, $lon, $ew, $speed, $true, $date, $variation, $ew2, $check) = split(',');
#$_ =~ s/(\n|\r)//g;

print "String Type: $string_type\n";
print FILE2 "String Type: $string_type\n";
print "Time: $timestamp\n";
print FILE2 "Time: $timestamp\n";
print "Validity: $validity\n";
print FILE2 "Validity: $validity\n";
print "Latitude: $lat\n";
print FILE2 "Latitude: $lat\n";
print "North/South: $ns\n";
print FILE2 "North/South: $ns\n";
print "Longitude: $lon\n";
print FILE2 "Longitude: $lon\n";
print "East/West: $ew\n";
print FILE2 "East/West: $ew\n";
print "Speed: $speed\n";
print FILE2 "Speed: $speed\n";
print "True Course: $true\n";
print FILE2 "True Course: $true\n";
print "Date: $date\n";
print FILE2 "Date: $date\n";
print "Variation: $variation\n";
print FILE2 "Variation: $variation\n";
#print "East/West: $ew2\n";
print "Check: $check\n";
print FILE2 "Check: $check\n";


print "---------\n";
}
close (FILE);
exit;

here is the output:

String Type: $GPRMC
Time: 081836
Validity: A
Latitude: 3751.65
North/South: S
Longitude: 14507.36
East/West: E
Speed: 000.0
True Course: 360.0
Date: 130998
Variation: 011.3
Check: *62
---------
String Type: 
Time: 
Validity: 
Latitude: 
North/South: 
Longitude: 
East/West: 
Speed: 
True Course: 
Date: 
Variation: 
Check: 
---------
String Type: $GPRMC
Time: 225446
Validity: A
Latitude: 4916.45
North/South: N
Longitude: 12311.12
East/West: W
Speed: 000.5
True Course: 054.7
Date: 191194
Variation: 020.3
Check: *68
---------
String Type: 
Time: 
Validity: 
Latitude: 
North/South: 
Longitude: 
East/West: 
Speed: 
True Course: 
Date: 
Variation: 
Check: 
---------
String Type: $GPRMC
Time: 081836
Validity: A
Latitude: 3751.65
North/South: S
Longitude: 14507.36
East/West: E
Speed: 000.0
True Course: 360.0
Date: 130998
Variation: 011.3
Check: *62
---------
String Type: 
Time: 
Validity: 
Latitude: 
North/South: 
Longitude: 
East/West: 
Speed: 
True Course: 
Date: 
Variation: 
Check: 
---------
String Type: $GPRMC
Time: 225446
Validity: A
Latitude: 4916.45
North/South: N
Longitude: 12311.12
East/West: W
Speed: 000.5
True Course: 054.7
Date: 191194
Variation: 020.3
Check: *68
---------
String Type: 
Time: 
Validity: 
Latitude: 
North/South: 
Longitude: 
East/West: 
Speed: 
True Course: 
Date: 
Variation: 
Check: 
---------
String Type: $GPRMC
Time: 081836
Validity: A
Latitude: 3751.65
North/South: S
Longitude: 14507.36
East/West: E
Speed: 000.0
True Course: 360.0
Date: 130998
Variation: 011.3
Check: *62
---------
String Type: 
Time: 
Validity: 
Latitude: 
North/South: 
Longitude: 
East/West: 
Speed: 
True Course: 
Date: 
Variation: 
Check: 
---------
String Type: $GPRMC
Time: 225446
Validity: A
Latitude: 4916.45
North/South: N
Longitude: 12311.12
East/West: W
Speed: 000.5
True Course: 054.7
Date: 191194
Variation: 020.3
Check: *68
---------

I want to get rid of those extra sections that are not printing any values. Any idea how to do this?

Add a next if m/^\s*$/; command in your while loop, immediately after the chomp; . Like this:

while (<FILE>) {
    chomp;
    next if m/^\s*$/;

next if m/^\s*$/; means that if the $_ variable matches a pattern of nothing but spaces (if anything) between the start and the end of the string then skip to the top of the loop to read the next input record from your file.

Add a next if m/^\s*$/; command in your while loop, immediately after the chomp; . Like this:

while (<FILE>) {
    chomp;
    next if m/^\s*$/;

next if m/^\s*$/; means that if the $_ variable matches a pattern of nothing but spaces (if anything) between the start and the end of the string then skip to the top of the loop to read the next input record from your file.

thank you! that worked.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.