Its PERL + Comma separated seventh field

Hi Friends,

I'm working on a perl script, which seems to be simpler. But I'm very new to PERL scripting.

I have a comma separated data file, from which I need to extract only the seventh field data out of available twenty fields to an array using perl.

Any help would be much appreciated.

With Regards / Mysore Ganapati.

then show what you have.....

below is the sample data:

,,,,,,32080808,,,,,,,,,,,,
,,,,,,72020807,,,,,,,,,,,,
,,,,,,42086776,,,,,,,,,,,,
.
.
.

how to store only the seventh field to an array?

What does your script look like?

Hi Sweetblood,

I just need an idea in perl to implement my requirement.

Thanks in advance.

Just split the input record and push the seventh field into an array.

tyler_durden

Ganapati, you will get much more help from people on this forum if you post what you
have come up with so far. People are more than willing to help but it can be diffcult to
see exactly where you need help without seeing what you have attempted so far.

A simple Web search will show you plenty of examples of how to do what you want to do.

Meanwhile, here is a pointer to Parsing CSV using Perl

I agree with this question.

ganapati, what code have you written or tried so far? It is best for you to try to write some code and show folks what you have attempted.

-----Post Update-----

Agreed. Thanks fpmurphy.

Dear friends,

As I told you earlier, I'm totally new to PERL. I know shell scripting very well, but now the requirement is PERL!!

I've to alter existing perl code and the only changes is to read the "Semicolon" separated file (sorry, not comma separated as mentioned earlier) and store the seventh field to an array for later comparison.

The problem I'm facing is, I don't know how to extract only the seventh field in perl. I know its very easy using AWK. But now the change is in existing perl script.

Hence requested for your kind help.

Thanks.

when you are new to something, you read up the docs, type this: perldoc perl
then try looking at this: perldoc -f split

I found the answer, struggling at the begining, you all suggested me.
Thanks for all.

my @arr_print = split( /;/, $line );
print "$arr_print[7]\n" ;

With Regards / Mysore Ganapati.

"There are no secrets to success. It is the result of preparation, hard work, and
learning from failure." - Colin Powell

If you only want the 8th field (7th index) from a list use a slice:

my $token7 = (split( /;/, $line))[7]; 
print "$token7\n" ;

Hi,

I've another problem with the scope of array. I'm not able to print array elements (for 8th field) outside the loop. But I can print this within the loop.

open( my $in, '<', $inputfilename )
or croak "Cannot open file '$inputfilename': $!";

my @orig_array;
while ( my $line = <$in> ) {
chomp $line;
@orig_array = split( /;/, $line );
print "$orig_array[7]\n" ; ## I can print elements here.
}
print "$orig_array[7]\n" ; ## I can't print elements here.

Please suggest me.

Hi Ganapati.

I think that problem due to scope of the array and its elements. Try that.
May be some experts can help you!!

All the best.

Assuming I understand your real question/problem, try this:

open( my $in, '<', $inputfilename )
    or croak "Cannot open file '$inputfilename': $!";

my @new_array;
while ( my $line = <$in> ) {
  chomp $line;
  my @orig_array = split( /;/, $line ); 
  push @new_array, $orig_array[7];
}
  print "$_\n" for @new_array;

Hi Kevin,

Thanks for your help. Your modification worked perfectly.

But, is there any way to avoid PUSH operation? since my file has more than 3-5 billions of records.

Regards,
Ganapati.

You could write $orig_array[7] to a file inside the loop. Then open the file and read the file outside the loop.

The problem with the printing in your original script was probably that the last record was empty (or newline or a blank or something). Your script was printing only the most recent record.

Kevin's suggestion is correct; but if you can't store 3-5 billions of records, then what exactly is your hope? Just print out the 8th column? If so, you could so:

cut -d \; -f 8

Of in perl:

perl -ne 'print (split(/;/))[7] . "\n";'