1753758 Members
4690 Online
108799 Solutions
New Discussion юеВ

Query on the file

 
SOLVED
Go to solution
Chandrakumar Karunamoor
Occasional Advisor

Query on the file

Hi all,
I have a comma delimited file in which the first field is a key field. I need to do something equivalent to the following in unix.

select count(*) from where is between and ;

The start and end values could be anything (except that end value is always greater). I need to split the file depending on the number i get from above.

In addition to the count if I get the line numbers of the records for the start and end that would be great.

Something worth mentioning here is the start and end value may or may not be in the file.

For example if below is the file

201,34934....
204,39438...
207,983493...
210,03943...
220,0983.....

If my start and end values are 202 and 215. The result needs to be 3 for the count and 2 and 4 for the line numbers.

Thank you very much for you help.
Yours Sincerely,
Chandra
7 REPLIES 7
harry d brown jr
Honored Contributor
Solution

Re: Query on the file

How about something as ugly as this:

#!/opt/perl/bin/perl
#
$begVAL=$ARGV[0];
$endVAL=$ARGV[1];
$filename=$ARGV[2];
open(fileptr,"<$filename");
#
$begno=0;
$endno=0;
$lineno=0;
#
while () {
$lineno += 1;
($fld1val, $fldrst) = split(/,/);
if (( $begVAL <= $fld1val ) && ( $begno == 0 )) {
$begno = $lineno;
}
if ( $endVAL >= $fld1val ) { $endno=$lineno; }
}
print "first is $begno\n";
print "last is $endno\n";
print "num lines is ",$endno-$begno+1,"\n";

./someperl.pl 202 215 ./numlist.txt
first is 2
last is 4
num lines is 3
#


live free or die
harry
Live Free or Die
A. Clay Stephenson
Acclaimed Contributor

Re: Query on the file

I would probably use awk for this:

Create an awk script, my.awk

BEGIN {
tot = 0
start = -1
end = -1
}
{
val = $1 + 0
if ((val >= lo) && (val <= hi))
{
++val
if ((start < 0) && (val == lo)) start = NR
if (val == hi) end = NR
}
}
END {
printf("%d lines found ",tot)
if (start >= 0) printf("Starting line %d ",start)
if (end >= 0) printf("Last key found %d",end)
printf("\n")
}

Then invoke awk thusly:

awk -F, -f my.awk -v "lo=202" -v "hi=215" myinputfile

If it ain't broke, I can fix that.
Rodney Hills
Honored Contributor

Re: Query on the file

"perl" does not have the 18 digit limit. If you can rewrite your script in "perl" you'll have other tools at your disposal too.

-- Rod Hills
There be dragons...
Leif Halvarsson_2
Honored Contributor

Re: Query on the file


Hi,
Or a shell script, call it "myscript" and do:
myscript



IFS=","
typeset -i cntr a
cntr=0

while read a b
do
if [[ $a -ge 202 && $a -le 215 ]]
then
cntr=$cntr+1
fi
done
echo $cntr


Chandrakumar Karunamoor
Occasional Advisor

Re: Query on the file

Thank you Harry and Clay.

I have a quick question in korn shell. I don't think korn shell supports integers of 18 digits. Is there a work around for this in korn shell itself.

Any help is greatly appreciated.

Yours sincerely,
Chandra
A. Clay Stephenson
Acclaimed Contributor

Re: Query on the file

There is no quick fix for that problem. The various shells support 32-bit signed integers whichs give you a range of ~ -2 billion .. +2 billion. If you need more than that then you can use bc for essentially unlimited precision and range.
If it ain't broke, I can fix that.
Leif Halvarsson_2
Honored Contributor

Re: Query on the file

Sorry,

I did not read your description carefully enough.
Here is an example shell script with start and end line numbers.


IFS=","
typeset -i cntr a start end line
cntr=0
start=0
end=0
line=0

while read a b
do
line=line+1
if [[ $a -ge 202 && $a -le 215 ]]
then
cntr=$cntr+1
if [ $start -eq 0 ]
then
start=$line
fi
end=$line
fi
done
echo Nr: $cntr Start: $start End: $end


No points for this .

Leif