954,523 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Read first line of a flat file

I would like to write Perl script to read just first line of a file & calculate the length of second field which is comma separator file.

For example -
1001,abcd,9999
1002,xyz,0001

expecting result - 4 (character length of second field from just first line i.e. abcd)

Can somebody help me to write perl script for this?

sandeepau
Newbie Poster
16 posts since Jul 2011
Reputation Points: 10
Solved Threads: 0
 
#!/usr/bin/perl;
use strict;
use warnings;

my $filename = 'input.csv';
open my $fh, '<', $filename or die "Unable to open $filename: $!";

my $firstline = <$fh>;
chomp($firstline);
print "The first line is $firstline\n";

my @fields = split(/,/, $firstline);
my $len = length($fields[1]);
print "The second field contains '$fields[1]' which has length of $len\n";
d5e5
Practically a Posting Shark
810 posts since Sep 2009
Reputation Points: 159
Solved Threads: 159
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You