gawk 上的真实数字
我在 Windows 计算机上使用 gawk 和 cygwin。 我想做的是从三列中找到最小值和最大值,其中两列是纬度和经度,第三列是值。
这是代码:
echo off
for /f "tokens=1,2,3 delims= " %%a in
('gawk "BEGIN {maxc = 0} {maxlo=0} {maxla=0}
{if ($3>maxc) maxc=$3} {if ($1>maxlo) maxlo=$1} {if ($2>maxla) maxla=$2} END
{print maxc, maxlo, maxla}" %file%.projected')
do (
set maxc=%%a
set maxlo=%%b
set maxla=%%c
)
echo max is %maxc%
echo max is %maxla%
echo max is %maxlo%
for /f "tokens=1,2,3 delims= " %%a in
('gawk "BEGIN {minc =1000} {minlo=1000} {minla=1000}
{if ($3<minc) minc=$3} {if ($1<minlo) minlo=$1} {if ($2<minla) minla=$2} END
{print minc, minlo, minla}" %file%.projected')
do (
set minc=%%a
set minlo=%%b
set minla=%%c
)
echo min %minc%
echo min %minla%
echo min %minlo%
我得到的是:
max is 465.053890
max latitude is 31.846428
max is 34.877658
min 19.976970
min 31.846428
min 34.877658
经度和纬度的最小值和最大值是相同的。 我如何比较实际数字?
I am using gawk on a windows computer with cygwin.
What i am trying to do is find min and max from three columns, two are lat and lon and the third column is the value.
This is the code:
echo off
for /f "tokens=1,2,3 delims= " %%a in
('gawk "BEGIN {maxc = 0} {maxlo=0} {maxla=0}
{if ($3>maxc) maxc=$3} {if ($1>maxlo) maxlo=$1} {if ($2>maxla) maxla=$2} END
{print maxc, maxlo, maxla}" %file%.projected')
do (
set maxc=%%a
set maxlo=%%b
set maxla=%%c
)
echo max is %maxc%
echo max is %maxla%
echo max is %maxlo%
for /f "tokens=1,2,3 delims= " %%a in
('gawk "BEGIN {minc =1000} {minlo=1000} {minla=1000}
{if ($3<minc) minc=$3} {if ($1<minlo) minlo=$1} {if ($2<minla) minla=$2} END
{print minc, minlo, minla}" %file%.projected')
do (
set minc=%%a
set minlo=%%b
set minla=%%c
)
echo min %minc%
echo min %minla%
echo min %minlo%
What i get is:
max is 465.053890
max latitude is 31.846428
max is 34.877658
min 19.976970
min 31.846428
min 34.877658
The min and max are the same for the lon and lat.
How can i compare real numbers?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我有理由确信这条线
不会做你想象的那样。 (坦率地说,我对它的编译感到惊讶,我不认为它会在 Kernighan AWK 中编译。)这里发生的很多事情实际上都在 Window
BAT
文件中?但这里有一些猜测。
minc
,然后每次设置minlo
和minla
每次读取一行。更好的是。
或者甚至
确认,尝试这样做作为对评论的回答。不去。
第一行不起作用的原因是 AWK 行具有以下形式
,其中 PATTERN 是某个表达式,用于标识代码部分操作的一组输入记录(通常是行)。有一些特殊情况,例如
BEGIN
,它在第一次读取之前“匹配”。另一个特殊情况是空模式,它匹配每一行。所以你所得到的被解释为所以,在处理第一行输入之前,
minc
设置为 1000。接下来,在 每个 > 输入行,minlo
和minla
设置为 1000。我的版本,因为它们都在BEGIN
行中进行初始化,只需初始化值一次。I'm reasonably sure that this line
doesn't do what you think it does. (Frankly, I'm astounded it compiled at all, I don't think it would have in Kernighan AWK.) And a lot of what's going on here is actually in, what, a Window
BAT
file?But here are some guesses.
minc
at start time, and then setsminlo
andminla
every time it reads a line. Better would be.
or even
Ack, tried to do this as an answer to a comment. No go.
The reason the first line doesn't work is that AWK lines have the form
where PATTERN is some expression that identifies a set of input records (usually lines) on which the code part operates. There are some special cases, for example
BEGIN
, which "matches" before the first time is read. Another special case is the empty pattern, which matches every line. So what you had was being interpreted asSo, before the first line of input is processed,
minc
is set to 1000. Following that, on every input line,minlo
andminla
are set to 1000. My versions, since they all do the initialization in aBEGIN
line, just initialize the values once.