Perl matching values in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl matching values in a file
# 1  
Old 05-21-2011
Wrench Perl matching values in a file

I am trying to match first 5 values of $passt variable in a file $chpdvlst and then verify that value in $grp parameter I want first 5 values which has $vaht - ship and $grp = N-grp. Somehow script below lists whole file.

Code:
 
$memcnt = 5;
$passt = ship;
open (ADVOUT, "< $chpdvlst") || die ("can not open file: $chpdvlst!");
while ($LINEIN2 = <ADVOUT>)
 {
  chomp ($LINEIN2);
  ($advid, $vaht, $grp, $xsinfo) = split(' ',$LINEIN2);
   $cnt1 = 0;
   
  if (($passt = $vaht) && ($cnt1 < $memcnt ))
     {
    
   # Check type
   if (($vaht =~ "ship") && ($grp=~ "N-Grp"))
   {
          print "$vaht $adevid ==> in free pool OK to use\n";
         $cnt1++;
   }
 
     }

$chpdvlst -- file i as follows.

289B ship N-grp 123
289C ship N-grp 123
289D ship N-grp 123
2CE1 flight N-grp 123
2CE2 flight N-grp 123
2CE3 flight N-grp 123
2CE4 flight N-grp 123
2DAF flight N-grp 123
2DB0 air Y-Grp 123
299B ship N-grp 123
299C ship N-grp 123
299D ship N-grp 123

Smilie
# 2  
Old 05-21-2011
Hi,

To help understand could you please give us the input file content, and the desired output ?
# 3  
Old 05-22-2011
Hi Chirel.

Here is the input format and expected output.

Input File -- mentioned in $chpdvlst
289B ship N-grp 123
289C ship N-grp 123
289D ship N-grp 123
2CE1 flight N-grp 123
2CE2 flight N-grp 123
2CE3 flight N-grp 123
2CE4 flight N-grp 123
2DAF flight N-grp 123
2DB0 air Y-Grp 123
299B ship N-grp 123
299C ship N-grp 123
29AD ship N-grp 123
29AB ship grp 123
29AC ship grp 123
29AD ship grp 123

OUTPUT Format
289B ===>Found in Free Pool
289C ===>Found in Free Pool
289D ===>Found in Free Pool
2DB0 ===>Found in Free Pool
299B ===>Found in Free Pool
299C ===>Found in Free Pool
29AD ===>Found in Free Pool
29AB ===>Not Found in free pool
29AC ===>Not Found in free pool
29AD ===>Not Found in free pool


I missed else statement in earlier code here is the code.

Code:
$memcnt = 5;
$passt = ship;
open (ADVOUT, "< $chpdvlst") || die ("can not open file: $chpdvlst!");
while ($LINEIN2 = <ADVOUT>)
 {
  chomp ($LINEIN2);
  ($advid, $vaht, $grp, $xsinfo) = split(' ',$LINEIN2);
   $cnt1 = 0;
  if (($passt = $grpt) && ($cnt1 < $memcnt ))
     {
    print " $advid ===>Found in Free Pool.\n";
    $cnt1++;
     }
   else
      {
        print "$adevid===>  NOT FOUND in Free Pool ---  Check Before Using\n";
      }

---------- Post updated at 11:23 PM ---------- Previous update was at 09:33 PM ----------

I mean in output I should see only first five lines

289B ===>Found in Free Pool
289C ===>Found in Free Pool
289D ===>Found in Free Pool
2DB0 ===>Found in Free Pool
299B ===>Found in Free Pool
# 4  
Old 05-22-2011
Hi

Try next 'Perl' script:
Code:
$ cat script.pl
use warnings;
use strict;
use autodie;

@ARGV == 1 or die "Usage: perl $0 input-file\n";
open my $fh, "<", $ARGV[0];

my $cnt = 5;
my @f;

while ( <$fh> ) {
    chomp;
    @f = split;
    if ( $f[1] eq "ship" && $cnt-- > 0 ) {
        print $f[0] . " ===> ";
        if ( $f[2] eq "N-grp" ) {
            print "Found in Free Pool\n";
        } else {
            print "Not Found in Free Pool\n";
        }
    }
    if ( $cnt == 0 ) { last; }
}

close $fh;
$ perl script.pl infile
289B ===> Found in Free Pool
289C ===> Found in Free Pool
289D ===> Found in Free Pool
299B ===> Found in Free Pool
299C ===> Found in Free Pool

Regards,
Birei
This User Gave Thanks to birei For This Post:
# 5  
Old 05-22-2011
Hi,

Try this
Code:
$memcnt = 5;
$passt = "ship";
$cnt1 = 0;
open (ADVOUT, "< $chpdvlst") || die ("can not open file: $chpdvlst!");
while (($LINEIN2 = <ADVOUT>) && ($cnt1 < $memcnt)) {

    chomp ($LINEIN2);

    ($advid, $vaht, $grp, $xsinfo) = split(' ',$LINEIN2);

    if (($passt eq $vaht) && ($grp eq "N-grp")) {
        $cnt1++;
        print "[$cnt1] $advid ===>Found in Free Pool.\n";
    }
}

This User Gave Thanks to Chirel For This Post:
# 6  
Old 05-22-2011
Thanks Chirel and birei. That was incredible!!!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How do delete certain lines alone which are matching with start and end string values in file?

Hi, In my previous post ( How to print lines from a files with specific start and end patterns and pick only the last lines? ), i have got a help to get the last select statement from a file, now i need to remove/exclude the output from main file: Input File format: SELECT ABCD, DEFGH,... (2 Replies)
Discussion started by: nani2019
2 Replies

2. Shell Programming and Scripting

How to find sum of any 'n' number of values from file matching target value?

I have a simple text file having payment amount value on each line. At the end of day 'n' number of payments created difference in amount that I need to match from this file. I have information about how many payments created difference and difference amount. Please help me to build shell... (3 Replies)
Discussion started by: swats007
3 Replies

3. Shell Programming and Scripting

PERL: matching text between 2 values

Hi, I am trying to get a text value between 2 words in a string and assign it to a value. Basically the program should read each row in a file and return the text between 2 fields and print it to another file. My code: #!/usr/bin/perl open FAILED, "./AFile.txt"; while(<FAILED>) { ... (3 Replies)
Discussion started by: chris01010
3 Replies

4. Shell Programming and Scripting

Compare values in two files. For matching rows print corresponding values from File 1 in File2.

- I have two files (File 1 and File 2) and the contents of the files are mentioned below. - I am trying to compare the values of Column1 of File1 with Column1 of File2. If a match is found, print the corresponding value from Column2 of File1 in Column5 of File2. - I tried to modify and use... (10 Replies)
Discussion started by: Santoshbn
10 Replies

5. Shell Programming and Scripting

Copy values from columns matching in those in second file.

Hi All, I have two sets of files. Set 1: 100 text files with extension .txt with names like 1.txt, 2.txt, 3.txt until 100.txt Set 2: One big file with extension .dat The text files have some records in columns like this: 0.7316431 82628 0.7248189 82577 0.7248182 81369 0.7222999... (1 Reply)
Discussion started by: shoaibjameel123
1 Replies

6. UNIX for Dummies Questions & Answers

PERL pattern matching in a file

Hi Gurus, I have a file like below.. I have to match each with predefined pattern. If matches then have to write the entire record to a separate file. If not make the value as NULL and write the entire record into another file. | is the delimiter ravi123|2344|M R123Vi|2345|F... (8 Replies)
Discussion started by: pvksandeep
8 Replies

7. Shell Programming and Scripting

perl -write values in a file to @array in perl

Hi can anyone suggest me how to write a file containing values,... say 19 20 21 22 .. 40 to an array @array = (19, 20, ... 40) -- Thanks (27 Replies)
Discussion started by: meghana
27 Replies

8. Shell Programming and Scripting

PERL:How to convert numeric values txt file to PACKED DECIMAL File?

Is there any way to convert numeric values txt file to PACKED DECIMAL File using PERL. Regards, Alok (1 Reply)
Discussion started by: aloktiwary
1 Replies

9. Shell Programming and Scripting

using perl for matching one file with another file and print into new line

One file is fileA 0.0246*0.0068*0.0013*0.0023*0.0182*0.0028*0.0019*0.4750*0.0028*0.0812*0.0123*0.0018*0.0039*0.0020*0.0028*0.0047*0.0139*0.3330*0.0017*0.0072*0.4789... (4 Replies)
Discussion started by: cdfd123
4 Replies

10. Shell Programming and Scripting

Identify matching data in a file and output to original line, in perl

Hi, I haven't done this for awhile, and further, I've never done it in perl so I appreciate any help you can give me. I have a file of lines, each with 5 data points that look like this: AB,N,ALLIANCEBERNSTEIN HLDNG L.P,AB,N ALD,N,ALLIED CAPITAL CORPORATION,ALD,N AFC,N,ALLIED CAPITAL... (4 Replies)
Discussion started by: Pcushing
4 Replies
Login or Register to Ask a Question