Reading from a file - by passing the value

Hi ,
I am writing data from into a file. This file is pipe demilited and every new data starts from a new line. First value of the line is unique value. I need to read this file and generate a report. Now my question is -

If i am having this file ready then how can i read the data by passing that unique key.

Format of the file is like this ->
12345|Tom|Jonh|Analyst|Software devision|$30000|5 Years|contract expires on 2132|end
-----------------------------------------------------------------
-----------------------------------------------------------------
------------------------------------------------------------------

Now i will pass the 12345 to this file to take the rest of the data i.e Tom , John , Analyst etc. in the same order which will be printed in another report.

Thanks in Advance !

Aaku
akku_batraAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

PurplePerlsCommented:
On windows do it like this:

type customer.dat | perl extract.pl 12346

Where the extract.pl is like this:

my $key = shift;
while (<>) {
  print unless !(/^$key/);
}


On Unix it is simelar command:
cat customer.dat | perl extract.pl 12346 >> report.dat




PurplePerlsCommented:
And if you like to remove the key and change the pipes to comas, then try this:

my $key = shift;
while (<>) {
  if(/^$key/){
    $_ =~ s/^\w+\|//;
    $_ =~ s/\|/\,/g;
    print;
  };
};



Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ozoCommented:
Better to use
/^$key\|/
Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

PurplePerlsCommented:
That's very true!
Worst case if , no key is entered, then are all records returned.

Here the corrected version:

my $key = shift;
while (<>) {
  if(/^$key\|/){
    $_ =~ s/^\w+\|//;
    $_ =~ s/\|/\,/g;
    print;
  };
};


Or without normalization:
my $key = shift;
while (<>) {
  print unless !(/^$key\|/);
}

kanduraCommented:
In that last bit i'd change the double negative from

   print unless !(/^$key\|/);

to

   print if /^$key\|/;

I never didn't like no double negatives ;^)
kanduraCommented:
Another note: if the file isn't too big (say, under 10MB), and you'd like to report on more than just a single line, then it may be easier to just slurp the whole file into a hash, where the keys are your unique first values, and the value is the rest of the line (possibly you'd even want the rest of the line split into an array as well, which I'll show too):

#!/usr/bin/perl -w

my $filename = 'data.txt';
my %lines = ();

open F, $filename or die $!;
while(<F>)
{
  chomp;                                             # remove newline
  my ($key, $data) = split /\|/, $_, 2;     # split on | into two parts
  $lines{$key} = $data;

  ### alternatively, store an array ref to the data
  my ($key, @flds) = split /\|/, $_;
   $lines{$key} = \@flds;
}
close F;

### now use %lines
my @somekeys = qw( 12345 12346 );   # some random keys ;^)
foreach(@somekeys)
{
    print $lines{$_}, "\n";
   
   ### alternatively
    print join(', ', @{$lines{$_}}), "\n";
}

ozoCommented:
#Another way to use a hash without using up space on large files is:
my $filename = 'data.txt';
my @somekeys = qw( 12345 12346 );   # some random keys ;^)
@hash{@somekeys} = (1)x@somekeys;

open F, $filename or die $!;
while( <F> ){
  my ($key, $data) = split /\|/, $_, 2;     # split on | into two parts
  print if $hash{$key};
close F;
TintinCommented:
PurplePerls.

No need to fork additional processes:

cat customer.dat | perl extract.pl 12346 >> report.dat

is better written as

perl extract.pl 12346 <customer.dat >> report.dat

No UUOC
PurplePerlsCommented:
Thanks.
Now I learned one more jargon acronym :)
jmcgOwnerCommented:
Nothing has happened on this question in more than 6 weeks. It's time for cleanup!

My recommendation, which I will post in the Cleanup topic area, is to
split points between PurplePerls and ozo.

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jmcg
EE Cleanup Volunteer
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.