Operating System - HP-UX
1752795 Members
6131 Online
108789 Solutions
New Discussion юеВ

Re: Perl script that detects a problem

 
SOLVED
Go to solution
yaron1
Advisor

Perl script that detects a problem

Perl script that detects a problem

I need a perl code that scans a huge input file and write to stdout or to another file the 3 following records:
1. The first 32 characters of the previous record.
2. The first 32 characters of the current record.
3. A blank line

The above 3 lines are to be written only on (a combination of all of) the following conditions:
1. The first two characters of the current line are 01, and
2. The first two characters of the previous line are 99, and
3. Positions 3 to 32 are equal in the current line to those of the previous line.

Thanks for the answers.
5 REPLIES 5
James R. Ferguson
Acclaimed Contributor
Solution

Re: Perl script that detects a problem

hi:

See if this fits your needs. Remember, Perl counts zero-relative. Hence when you say "positions 3-32" I translated this to 2-31 zero-relative.

# cat ./filter
#!/usr/bin/perl
use strict;
use warnings;
my $prev = q();
while (<>) {
chomp;
if ( substr( $_, 0, 2 ) eq "01"
and substr( $prev, 0, 2 ) eq "99"
and substr( $_, 2, 31 ) eq substr( $prev, 2, 31 ) )
{
print substr( $prev, 0, 31 ), "\n", substr( $_, 0, 31 ), "\n\n";
}
$prev = $_;
}
1;

...run as:

# ./filter file

Regards!

...JRF...
yaron1
Advisor

Re: Perl script that detects a problem

It├в s true about the positions.
It doesnt work. When I put this code in file ├в filter├в and then in the HP-UX command line I type:
./filter tst4
I get:
./filter[3]: use: not found.
./filter[4]: use: not found.
./filter[5]: Syntax error at line 5 : `(' is not expected.

If I put the code in file filter.pl (and remove the first 2 lines) and run as:
perl filter.pl tst4
I get nothing but I know this condition is met in the input file.

Thanks.
James R. Ferguson
Acclaimed Contributor

Re: Perl script that detects a problem

Hi (again):

> It doesnt work. When I put this code in file ...and then in the HP-UX command line I type:
./filter tst4
I get:
./filter[3]: use: not found.
./filter[4]: use: not found.
./filter[5]: Syntax error at line 5 : `(' is not expected.

Yes, you would get that if you _REMOVED_ the interpreter ("shebang") line: #!/usr/bin/perl . In this case the shell thought that the script was a shell script!

Every script should begin with a line like that. For a shell script you would use something like:

#!/usr/bin/sh

For a Perl script, you would use something like:

#!/usr/bin/perl

Regards!

...JRF....
yaron1
Advisor

Re: Perl script that detects a problem

I had removed the #!/usr/bin/perl only from the file I ran as a parameter to perl, Like that:
perl filter.pl tst4
I did try to run it also with the #!/usr/bin/perl line and without the perl, like that:
filter tst4

shouldnt both ways be the same to run a perl script?

I am using POSIX shell.

Thanks.
James R. Ferguson
Acclaimed Contributor

Re: Perl script that detects a problem

Hi (again):

> I had removed the #!/usr/bin/perl only from the file I ran as a parameter to perl, Like that: perl filter.pl tst4

Yes, either of these are equivalent:

# perl filter.pl tst4

# ./filter.pl tst4

[...where the latter starts with an interpreter line...]

#!/usr/bin/perl

If you don't know where Perl lives (or it differs on different servers) you can use this shebang:

#!/usr/bin/env perl

Notice that there is a whitespace after 'env' and before 'perl'.

Regards!

...JRF...