georgetovrea
georgetovrea

Reputation: 577

How to write each row of a text file into CSV

I have a netflow information report in text file, and I want to export them to csv format, how do I do that in Perl? I am new to Perl, just start to learn it.

Here is the text file:

Duration    Flags    
10.10.10.1      10.10.11.11    51251    445      2019-03-05 11:59:29.008   29.156     ......
10.10.10.2      10.10.11.22    51234    123      2019-03-05 11:40:29.008   30.156     .A..SF

I want to export this to excel format as follows.

10.10.10.1,10.10.11.11,51251,445,2019-03-05 11:59:29.008,29.156,......  
10.10.10.2,10.10.11.22,51234,123,2019-03-05 11:59:29.008,30.156,.A..SF  

I have try to do as below,

use strict;
use warnings;

open my $fh, '<', 'file.txt';
open my $fo, '>', 'file.csv';
while (<$fh>) {
   next if ($. == 1);
   s/\b \b/,/g;
   my $text = (join ',', (split /\s+/, $_))."\n";
   print $fo $text;
}

but the results is every line of start will be add one more ',' ,as below,

,10.10.10.1,10.10.11.11,51251,445,2019-03-05 11:59:29.008,29.156,......  
,10.10.10.2,10.10.11.22,51234,123,2019-03-05 11:59:29.008,30.156,.A..SF  

This is what I wants and what's problem with my code ?

10.10.10.1,10.10.11.11,51251,445,2019-03-05 11:59:29.008,29.156,......  
10.10.10.2,10.10.11.22,51234,123,2019-03-05 11:59:29.008,30.156,.A..SF  

Anyway of doing this?? Thank you.

Upvotes: 1

Views: 103

Answers (2)

sergiotarxz
sergiotarxz

Reputation: 540

May something like this work?

use strict;
use warnings;

open my $fh, '<', 'text.txt';
open my $fo, '>', 'text.csv';
while (<$fh>) {
    s/^\s+//;
    s/\b \b/_/g;
    my $text = (join ',', (split /\s+/, $_))."\n";
    print $fo $text;
}

Upvotes: 0

Dave Cross
Dave Cross

Reputation: 69244

There are several steps to this problem. Rather than just handing the answer to you, I'm going to talk you through the steps and point you at the documentation for some functions that will help you at each stage.

  1. You'll need to open two filehandles - one to read your data from and one to write your (transformed) data to. The open() function is used to open a file and attach that file to a filehandle which you can use to read data from the file.
  2. You'll then want to read data from your input filehandle. You can use the file input operator (< ... >) for that.
  3. You'll then want to split your input line apart into the individual fields. Perl has a cunningly-named split() function for doing that.
  4. You'll then want to construct a CSV record from your split-up input data. You might consider using the Text::CSV module for that, but in this (simple) case, you could just use join().
  5. You'll want to write your CSV record to your output file. You do that using print().
  6. Finally, you'll want to close your two filehandles - which you do with close().

I've deliberately left one interesting corner of your problem unmentioned. Everyone needs one meaty problem to get their teeth into, right? :-)

Have a go at writing something based on this information and, if you have more questions, feel free to ask them here.

Upvotes: 5

Related Questions