Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

trailer row does not match count of delimited char in file

Status
Not open for further replies.

pearlofperls

Technical User
Apr 5, 2007
24
US
I'm trying to make sure trailer row of a file matches the count of delimited character in the file.

The command "cut -f 2 -d, TEMPFILE | grep CHARACTER1 | wc -l"
output is 38147

The command "cut -f 2 -d, TEMPFILE | grep CHARACTER2 | wc -l"
output is 64966


Here is the output for tailing the last line of TEMPFILE:

tail -1 TEMPFILE
001,9,MDBK,610,0038147,0064966,O,20070331,025200,P

How would I construct a perl scriptt to be call in a ksh script to ensure the trailer row is correct?
 
grep finds lines that contain matches. So if a line matches the character in more than one place, you still only get one line output. So your counts are actually the number of lines that match, not a count of the matches.
Code:
use warnings;
use strict;

my @charCounts;

while (<>) {
   if (eof()) {
      my @trailerCounts = (split /,/) [4, 5];
      for (0 .. 1) {
         unless (($charCounts[$_] == $trailerCounts[$_]) {
            die "Expected: $charCounts[$_], trailer: $trailerCounts[$_]"
         }
      }
   }
   $charCounts[0]++ if /CHARACTER1/;
   $charCounts[1]++ if /CHARACTER2/;
}
(not tested)


Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Thanks Steve.

The way I'm reading this is this will just check if the value in the trailer row will match the count. I need to make sure it overwrites the correct value.
 
Steve can you add one line of code to overwrite the charocunt to trailer row.

thanks!
 
Unless I'm mistaken
I'm trying to make sure trailer row of a file matches the count of delimited character in the file
was your original requirement. Be more specific next time [smile]

I'll have a look at it this evening.

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Code:
#!/usr/bin/perl
use strict;
use warnings;

my @charCounts = (0, 0);

while (<>) {
   if (eof) {
      my @trailerCounts = (split /,/);
      $trailerCounts[4] = sprintf("%07s", $charCounts[0]);
      $trailerCounts[5] = sprintf("%07s", $charCounts[1]);
      print join(",", @trailerCounts);
      last;
   }
   $charCounts[0]++ if /CHARACTER1/;
   $charCounts[1]++ if /CHARACTER2/;
   print $_;
}
Not the prettiest code ever, but it does the job. Note, you will have to pipe the output to a file. I don't like updating files in place, if things go wrong you lose the data...

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top