3 Replies - 756 Views - Last Post: 09 May 2016 - 12:18 PM

#1 wamBamThankYouSam  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 09-May 16

Perl script return number smaller than it should (grep)

Posted 09 May 2016 - 08:33 AM

#!/usr/bin/perl -w
open("file", "<text.txt");      #
        @file = <file>;         #file looking into
        close "file";           #
while(<>){
        $temp = $_;
        $temp =~ tr/|/\t/;      #puts tab between name and id
        my ($name, $number1, $number2) = split("\t", $temp);
        if ( grep( /^$number1$/, @file) ) {
                print $_;
        }
}
print "\n";



It is taking input in the form of
Mac|49043 589
Will|54432 672
Yazi|55322 797
Gladys|79322 205
Gregger|5132 1907
Sam|55343 1534

And text .txt just has a bunch of numbers like
83293
29403
23484
32444

The problem is that it is outputting about 2100 lines when there are about 2500 lines that should match between the files

Is This A Good Question/Topic? 0
  • +

Replies To: Perl script return number smaller than it should (grep)

#2 jon.kiparsky  Icon User is offline

  • Screw Trump (before he screws you)
  • member icon


Reputation: 10624
  • View blog
  • Posts: 18,180
  • Joined: 19-March 11

Re: Perl script return number smaller than it should (grep)

Posted 09 May 2016 - 12:08 PM

2500 lines is a lot to look at. I would suggest you start by making a smaller test case. Pull out maybe three examples that behave correctly and three that do not, and use those for your test data. Post that test data here, so we can have a look.
Was This Post Helpful? 0
  • +
  • -

#3 jon.kiparsky  Icon User is offline

  • Screw Trump (before he screws you)
  • member icon


Reputation: 10624
  • View blog
  • Posts: 18,180
  • Joined: 19-March 11

Re: Perl script return number smaller than it should (grep)

Posted 09 May 2016 - 12:14 PM

That being said, it looks like you're expecting to find a tab separation between the two parts of the id number. Is this reasonable? What if you split on whitespace instead of just on tab? Does this resolve the problem?
(a file format that distinguishes between spaces and tabs is not a Good Thing, as Stuart Feldman can testify)
Was This Post Helpful? 0
  • +
  • -

#4 no2pencil  Icon User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6543
  • View blog
  • Posts: 30,648
  • Joined: 10-May 07

Re: Perl script return number smaller than it should (grep)

Posted 09 May 2016 - 12:18 PM

Your grep statement explicitly states start of line & end of line on each end of the text. Thus your script is saying ONLY give me lines that match EXACTLY this. Want to find the missing 400 items? send grep output to dev null, test the return value, & print the grep results upon failure.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1