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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

select numbers

Status
Not open for further replies.

tonivm

Technical User
Mar 2, 2006
64
ES
hi everybody:
I have a file as:
......
0.873
0.937
0.997
1.057
1.123
1.183
1.253
1.309
1.365
1.426
1.488
......

and I would like to select the minimum and maximum from certain values as 1, 1.1, 1.2 so on to 4.5. This is in the first case 0.997<1<1.057.

Thanks in advance and cheers. :)
 
And what have you tried so far ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
for i in `ls *.txt`
do
fitxer=`echo $i | cut -c 1-8`;
LC_ALL=en_DK gawk 'BEGIN{FS = ","; OFS = ",";zM=0; zm=0};
{for (j=1; j<=NF; j++ )
{for (i=1; i=4.5; i+=0.1)
{
if ( $1 <= i) {$1=zM; tM=$3; whM=$6};
if ( $1 >= i) {$1=zm; tm=$3; whm=$6};
};
{
ti = (tm-tM)*i+tm;
whi = (whm-whM)*i+whm;
print i, ti, whi
};
}
}' $i > $fitxer.atms

done
 
I can't make sense of the requirement... for the sample input you provided, what output would you expect?

Annihilannic.
 
Hi:
I try to do lineal interpolation of the second field:
...............
0.943,290.25
0.997,289.75
1.053,289.25
1.105,288.85
1.157,288.75
1.215,289.25
1.263,289.05
1.316,289.05
1.368,288.65
1.418,288.25
1.472,287.75
1.523,287.45
1.577,287.15
..............
when first field is 1, 1.1, 1.2, 1.3 ........
 
For that input, what output do you expect? I don't understand 'lineal interpolation'.

Annihilannic.
 
For a file where first field is distance and second field is temperature, I would like top know how from certain fixed values between [1 to 4.5] with a same increment step.

Then for example for i=1 (distance) the value os temperature is between :

0.997,289.75
1.053,289.25

then:
zmin=0.997; zmax=1.053
ti=(289.25-289.75)*1+289.25
 
I presume you mean 'linear interpolation'... by my calculation the result for those values should be ~289.723, using this formula:

[tt]zmin=0.997; zmax=1.053
tmin=289.75; tmax=289.25
(((i-zmin)/(zmax-zmin))*(tmax-tmin))+tmin[/tt]

Have I misunderstood what you are trying to do?

Annihilannic.
 
I only want to know how can I select this zmin and zmin to obtain this interpolation.
 
Try something like this:

Code:
awk -F, '
        {
                bucket=sprintf("%d",$1*10)+0
                if (maxbucket=="" || bucket > maxbucket) {
                        maxbucket=bucket
                }
                if (minbucket=="" || bucket < minbucket) {
                        minbucket=bucket
                }
                if (maxdist[bucket]=="" || $1 > maxdist[bucket]) {
                        maxdist[bucket]=$1
                }
                if (mindist[bucket]=="" || $1 < mindist[bucket]) {
                        mindist[bucket]=$1
                }
                temp[$1]=$2
        }
        END {
                for (i=minbucket+1; i<=maxbucket; i++) {
                        zmin=maxdist[i-1]
                        zmax=mindist[i]
                        tmin=temp[ zmin ]
                        tmax=temp[ zmax ]
                        print i/10,zmin,zmax,tmin,tmax,((((i/10)-zmin)/(zmax-zmin))*(tmax-tmin))+tmin
                }
        }
'

Basically it uses buckets of dist*10 and stores the minimum and maximum values in those ranges.

Annihilannic.
 
Hi Annihilannic:
I 've tried to use this script, but doesn't work correctly.
Really I have a file as:

0.098,1007.5,293.35,77,289.25,13.5252
0.157,1000.7,293.75,72,288.55,12.9151
0.21,994.6,293.55,69,287.65,12.1961
0.259,989.0,293.95,66,287.35,11.9453
0.307,983.5,293.65,66,287.15,11.8034
0.36,977.5,293.55,65,286.75,11.5045
0.413,971.6,293.45,65,286.65,11.4337
0.466,965.7,293.35,65,286.55,11.3633
0.52,959.5,292.85,64,285.85,10.8734
0.579,953.0,292.35,65,285.65,10.75
0.634,947.0,292.25,57,283.55,9.35738
0.682,941.7,291.85,56,282.95,9.00124
0.73,936.4,291.55,53,281.85,8.36647
0.785,930.5,291.15,48,280.05,7.40988
0.837,924.8,290.85,45,278.85,6.82761
0.891,919.0,290.55,42,277.55,6.242
0.943,913.4,290.25,40,276.55,5.82345
0.997,907.6,289.75,43,277.15,6.08576
1.053,901.6,289.25,43,276.65,5.88505
1.105,896.1,288.85,44,276.65,5.8932
1.157,890.7,288.75,32,272.15,4.26334
1.215,884.6,289.25,32,272.55,4.38255
1.263,879.7,289.05,38,274.75,5.14362
1.316,874.1,289.05,40,275.45,5.40796
1.368,868.8,288.65,42,275.85,5.57205
1.418,863.7,288.25,47,277.05,6.07451
1.472,858.2,287.75,49,277.15,6.12806
1.523,853.0,287.45,45,275.75,5.55563
1.577,847.6,287.15,40,273.75,4.81746
1.636,841.6,287.75,13,259.65,1.62399
1.694,835.9,288.25,7,252.65,0.89756
1.746,830.8,288.05,11,257.85,1.39847
1.805,825.0,287.55,16,262.05,1.97361
.
.
.
.
.

then z=$1 and t=$3. If I modify only $2 for $3 the script shows:

1 0.098,1007.5,293.35,77,289.25,13.5252 0
1.1 0.098,1007.5,293.35,77,289.25,13.5252 0.157,1000.7,293.75,72,288.55,12.9151 0
1.2 0.157,1000.7,293.75,72,288.55,12.9151 0.21,994.6,293.55,69,287.65,12.1961 0
1.3 0.259,989.0,293.95,66,287.35,11.9453 0.307,983.5,293.65,66,287.15,11.8034 0
1.4 0.36,977.5,293.55,65,286.75,11.5045 0.413,971.6,293.45,65,286.65,11.4337 0
1.5 0.466,965.7,293.35,65,286.55,11.3633 0.52,959.5,292.85,64,285.85,10.8734 0
1.6 0.579,953.0,292.35,65,285.65,10.75 0.634,947.0,292.25,57,283.55,9.35738 0
1.7 0.682,941.7,291.85,56,282.95,9.00124 0.73,936.4,291.55,53,281.85,8.36647 0
1.8 0.785,930.5,291.15,48,280.05,7.40988 0.837,924.8,290.85,45,278.85,6.82761 0
1.9 0.891,919.0,290.55,42,277.55,6.242 0.943,913.4,290.25,40,276.55,5.82345 0
2 0.997,907.6,289.75,43,277.15,6.08576 1.053,901.6,289.25,43,276.65,5.88505 0
2.1 1.053,901.6,289.25,43,276.65,5.88505 1.105,896.1,288.85,44,276.65,5.8932 0
2.2 1.157,890.7,288.75,32,272.15,4.26334 1.215,884.6,289.25,32,272.55,4.38255 0
2.3 1.263,879.7,289.05,38,274.75,5.14362 1.316,874.1,289.05,40,275.45,5.40796 0
2.4 1.368,868.8,288.65,42,275.85,5.57205 1.418,863.7,288.25,47,277.05,6.07451 0
2.5 1.472,858.2,287.75,49,277.15,6.12806 1.523,853.0,287.45,45,275.75,5.55563 0
2.6 1.577,847.6,287.15,40,273.75,4.81746 1.636,841.6,287.75,13,259.65,1.62399 0
2.7 1.694,835.9,288.25,7,252.65,0.89756 1.746,830.8,288.05,11,257.85,1.39847 0
2.8 1.746,830.8,288.05,11,257.85,1.39847 1.805,825.0,287.55,16,262.05,1.97361 0
2.9 1.863,819.3,286.95,23,266.15,2.7305 1.924,813.4,286.35,30,269.15,3.43973 0
3 1.978,808.2,285.75,32,269.55,3.55216 2.033,802.9,285.25,35,270.25,3.74969 0
3.1 2.09,797.4,284.85,30,267.85,3.13371 2.141,792.6,284.65,26,265.85,2.68941 0
.
.
.
13.7 12.678,182.5,211.55,38,204.25,0.00534579 12.727,181.1,211.65,35,203.75,0.00496974 0
13.8 12.772,179.8,211.75,31,202.95,0.00441957 12.822,178.3,212.15,27,202.25,0.00397883 0

Then it doesn't appear the interpolation as well I only would like that it works between 1 to 4.5 as distance.
 
It looks like you missed the -F, parameter?

To only process the range from 1 to 4.5, just update the limits in the for loop, noting that they are multiplied by 10, for e.g. (including some other improvements):

Code:
awk [COLOR=red]-F,[/color]  '
        { bucket=sprintf("%d",$1*10)+0 ; temp[$1]=$3 }
        maxbucket=="" || bucket > maxbucket { maxbucket=bucket }
        minbucket=="" || bucket < minbucket { minbucket=bucket }
        maxdist[bucket]=="" || $1 > maxdist[bucket] { maxdist[bucket]=$1 }
        mindist[bucket]=="" || $1 < mindist[bucket] { mindist[bucket]=$1 }
        END {
                for (i=[COLOR=red]10[/color]; i<=[COLOR=red]45[/color]; i++) {
                        zmin=maxdist[i-1]
                        zmax=mindist[i]
                        tmin=temp[ zmin ]
                        tmax=temp[ zmax ]
                        print i/10,zmin,zmax,tmin,tmax,((((i/10)-zmin)/(zmax-zmin))*(tmax-tmin))+tmin
                }
        }
'

Annihilannic.
 
Another little question:
If now I would like to do the same but the "i" has differents values like 4.61, 4.71, 4.83. I tried to do with if stament as:

awk -F, '
{ bucket=sprintf("%d",$1*10)+0 ; temp[$1]=$3 }
maxbucket=="" || bucket > maxbucket { maxbucket=bucket }
minbucket=="" || bucket < minbucket { minbucket=bucket }
maxdist[bucket]=="" || $1 > maxdist[bucket] { maxdist[bucket]=$1 }
mindist[bucket]=="" || $1 < mindist[bucket] { mindist[bucket]=$1 }
END {
if (i==46.1 || i==47.1 || i==48.3) {
zmin=maxdist[i-1]
zmax=mindist
tmin=temp[ zmin ]
tmax=temp[ zmax ]
print i/10,zmin,zmax,tmin,tmax,((((i/10)-zmin)/(zmax-zmin))*(tmax-tmin))+tmin
}
}
'

but it did not works.
How can I solve it?.
Thanks in advance.
 
It's not just a simple modification to the original script. Try this:

Code:
awk -F, -v target=[COLOR=red]4.61[/color]  '
        $1 < target { prev=$1 ; tempprev=$3 ; next }
        $1 > target {
                print target,prev,$1,tempprev,$3,
                        (((target-prev)/($1-prev))*($3-tempprev))+tempprev
                exit
        }
' inputfile

Just change the part in red to the value for which you wish to find the interpolated temperature.

Annihilannic.
 
hi another:
I try to do the same thing that in the last reply.
I have a file with only two rows like this:

0 286.69
1 288.844

Now I would like interpolate beteween 0 to 1, but each 0.1.
In this case I did:

height=( 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9);
${height[@]};

for height in ${height[@]}
do
awk -F, -v target=$height '
$1 < target { prev=$1 ; tempprev=$2 ; next }
$1 > target { print target,
(((target-prev)/($1-prev))*($2-tempprev))+tempprev
exit
}' file1.dat >> file2.dat
done

Then I have, first this message:

./gir_int.sh: line 65: 0.1: command not found

and after the output file is:

0.1 0
0.2 0
0.3 0
0.4 0
0.5 0
0.6 0
0.7 0
0.8 0
0.9 0

Anybody knows where is the error.
Thanks in advance and cheers. :)
 
This line is giving you the error:

[tt]${height[@]};[/tt]

What are you trying to do there? The shell tries to execute a command 0.1 0.2 0.3 ..., which of course is not recognised.

Also, in your for loop you can't use the same variable name as your array name. Try using for h in ${height[@]}.

Annihilannic.
 
Thanks a lot, but I've solved it by other way as:

awk ' {if (NR == 1){zmin=$1; t1=$2;};if (NR == 2){zmax=$1; t2=$2;};}
END{ for (i=1; i<=9; i++)
{print i/10,((((i/10)-zmin)/(zmax-zmin))*(t2-t1))+t1 }}' file1.dat > file2.dat

I did that because as the original file has got only two rows and these are the first and second the minimum and maximum respectively. Also it works correctly.
Nevertheless thanks again.
Cheers. :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top