Parse Column information from CSV basics
I have a csv file in the following format(open in excel) Example:
Column A Column B Column C
192.168.1.100 172.16.16.1 WWW
192.168.1.100 172.16.16.2 abc
192.168.1.100 172.16.16.3 def
I am writing a Perl program that will get the information from column B, and convert it to an URL.
开发者_StackOverflow中文版I have tried a lot of methods such as split
, but I could not get any of them to work.
my $file = 'file.csv';
my $csv = Text::CSV->new(); while ( my $row = $csv->getline( $fh ) )
When I was googling for code. The code above is similar as the code I used before, but ended up giving some errors which I could not solve.
I use the following code instead:
open(IN, "LogFile1.csv");
while() { ... }
Inside the while {}
I did enter codes that delete duplicate row and do a convert to URL. It parse all information from the csv file. I tried to remove all column except column B and the codes work perfectly in converting the URL. Meaning when there 3 columns of info, converting IP to URL fails.
If it is a CSV file you can use Text::CSV to parse the data out. Something like:
#!/usr/bin/perl
use strict;
use warnings;
use Text::CSV;
my $file = 'file.csv';
my $csv = Text::CSV->new();
open my $fh, "<", $file or die $!;
while ( my $row = $csv->getline( $fh ) ) {
print 'http://' . $row->[1] . "\n";
}
close $fh;
You could do:
perl -ane '@F == 3 && print "http://$F[1]\n"' file.txt
精彩评论