1753873 Members
7870 Online
108809 Solutions
New Discussion юеВ

Perl conditions HELP

 
Pando
Regular Advisor

Perl conditions HELP

Dear Gurus,

Below is line part of my script:

export Var=$(awk -F "," '/DIFFLOTID/ {print $2}' $FILE)
$perl/perl -e '$PerlVar=$ENV{Var};
$PerlVar=~ m/^[0-9][0-9]*[\.[0-9][0-9]*]{0,1}$/g;
if (pos($PerlVar)) {system("'$scrptpath'/parset2xml -t '$FILE'");system("'$ExePath'/rm '$FILE'");else {system("'$ExePath'/mv -f '$FILE' '$temp'");'

The Var variable will get the DIFFLOTID, then it will be check if it is purely numeric. If it is numeric, then it will call parset2xml program and then remove the file to the system. If the DIFFLOTID is not purely numeric, it will move the file to a temp directory.

I dont know what is wrong with the script lines above.

Maximum points for all correct answers.
7 REPLIES 7
RAC_1
Honored Contributor

Re: Perl conditions HELP

No perl here, but I check if variable is number or not as follows.

if [ "$(echo $MYVAR | tr -d '[:digit:]')" = "" ]
then
echo "All digits"
else
echo "$MYVAR has non-digits"
fi


or

typeset newvar=$(var) 2>/dev/null
if [[ $? ├в eq 0 ]];then
echo ├в variable-${var} is interget├в
else
echo ├в not iteger├в
done
There is no substitute to HARDWORK
Pando
Regular Advisor

Re: Perl conditions HELP

Hello RAC,

Thanks for your quick reply. May i ask what is wrong with the line:

if (pos($PerlVar)) {system("'$scrptpath'/parset2xml -t '$FILE'");system("'$ExePath'/rm '$FILE'");else {system("'$ExePath'/mv -f '$FILE' '$temp'");'

Muthukumar_5
Honored Contributor

Re: Perl conditions HELP

Can you share DIFFLOTID format here. You are combining awk, shell and perl scripting. You can do it with awk or shell or perl with this.

Post that information to give script to that.

hth.
Easy to suggest when don't know about the problem!
Muthukumar_5
Honored Contributor

Re: Perl conditions HELP

May be your requirement is like this:

# cat filename
DIFFLOTID,12[12]
# cat test.pl
#!/usr/contrib/bin/perl
open FD, "filename" || die "Can not open file: $!";

while ()
{
$Var=(split (/,/))[1] if /DIFFLOTID/;
print "$Var\n";
}

print $var . "\n";
if ( $Var =~ m/^[0-9][0-9]*[\.[0-9][0-9]*]{0,1}$/g )
{
print "ok done" . "\n";
}
else {
print "sorry\n";
}
# perl test.pl
12[12]


ok done

# Just change your content as,

#!/usr/contrib/bin/perl
open FD, "filename" || die "Can not open file: $!";

while ()
{
$Var=(split (/,/))[1] if /DIFFLOTID/;
print "$Var\n";
}

print $var . "\n";
if ( $Var =~ m/^[0-9][0-9]*[\.[0-9][0-9]*]{0,1}$/g )
{
system("'$scrptpath'/parset2xml -t '$FILE'");system("'$ExePath'/rm '$FILE'");
}
else {system("'$ExePath'/mv -f '$FILE' '$temp'");'
}

##

That is it.



Easy to suggest when don't know about the problem!
Muthukumar_5
Honored Contributor

Re: Perl conditions HELP

Use this script:

#!/usr/contrib/bin/perl
$FILE="filename"
open FD, $FILE || die "Can not open file: $!";

while ()
{
$Var=(split (/,/))[1] if /DIFFLOTID/;
print "$Var\n";
}

print $var . "\n";
if ( $Var =~ m/^[0-9][0-9]*[\.[0-9][0-9]*]{0,1}$/g )
{
system("'$scrptpath'/parset2xml -t '$FILE'");
system("'$ExePath'/rm '$FILE'");
}
else {system("'$ExePath'/mv -f '$FILE' '$temp'");'
}

exit 0
##

Took more attempts to get this ;(
Easy to suggest when don't know about the problem!
Hein van den Heuvel
Honored Contributor

Re: Perl conditions HELP


From a perl perspective, the biggest thing that is wrong with your script is that is that is really is a shell script more than a perl program. You can do it al in perl, as written earlier.

Does the input to be tested for look like:
DIFFLOTID 123.455
followed by a ] or not?

I think the single regexpr for that, testing that word followed by the value is readably written as:

/DIFFLOTID\s+\d+\.\d+]?

that is:
- word
- one or more whitespace chars
- one or more decimal
- a period
- one or more decimal
- zero or one ]

If you prefer to do the action in the shell then you could do something like:

perl -ne '$x=1 if (/DIFFLOTID\s+\d+\.\d+]?/); print $x if eof' $FILE

hth,
Hein.





James R. Ferguson
Acclaimed Contributor

Re: Perl conditions HELP

Hi:

In a shell script one way to do this appears below. For example, to test whether or not ${X} is numeric (0..9):

# X=123
# if [ `expr ${X} : '[0-9]*'` -eq `expr ${X} : '.*'` ]; then
> echo "is_numeric"
> else
> echo "is_NOT_numeric"
> fi

This works by returning and comparing the number of characters in each 'expr'ession. If the number of *numeric* characters in the first expression is equal to the total number of characters in the second expresssion, then the first expression must consist only of (decimal) digits and therefore be "numeric".

Regards!

...JRF...