gawk 上的真实数字

发布于 2024-09-29 13:57:29 字数 1078 浏览 5 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怀念你的温柔 2024-10-06 13:57:29

我有理由确信这条线

'gawk "BEGIN {minc =1000} {minlo=1000} {minla=1000}

不会做你想象的那样。 (坦率地说,我对它的编译感到惊讶,我不认为它会在 Kernighan AWK 中编译。)这里发生的很多事情实际上都在 Window BAT 文件中?

但这里有一些猜测。

  • 我认为上面引用的行在开始时设置 minc ,然后每次设置 minlominla 每次读取一行。更好的是

 'BEGIN { minc = 1000; minlo = 1000; minla = 1000; } ....

或者甚至

 'BEGIN { minc = minlo = minla = 1000; } ....
  • 在 AWK 中,您也必须小心比较,因为如果您不小心,它会将您的数字强制为字符串,并进行字符串比较(即字典顺序)而不是数字比较。

确认,尝试这样做作为对评论的回答。不去。

第一行不起作用的原因是 AWK 行具有以下形式

PATTERN { code }

,其中 PATTERN 是某个表达式,用于标识代码部分操作的一组输入记录(通常是行)。有一些特殊情况,例如BEGIN,它在第一次读取之前“匹配”。另一个特殊情况是空模式,它匹配每一行。所以你所得到的被解释为

BEGIN {minc  =1000}
      {minlo =1000}
      {minla =1000}

所以,在处理第一行输入之前,minc 设置为 1000。接下来,在 每个 > 输入行,minlominla 设置为 1000。我的版本,因为它们都在 BEGIN 行中进行初始化,只需初始化值一次。

I'm reasonably sure that this line

'gawk "BEGIN {minc =1000} {minlo=1000} {minla=1000}

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.

  • I think the line I quoted above sets minc at start time, and then sets minlo and minla every time it reads a line. Better would be

.

 'BEGIN { minc = 1000; minlo = 1000; minla = 1000; } ....

or even

 'BEGIN { minc = minlo = minla = 1000; } ....
  • in AWK you have to watch out a little with comparisons, because if you aren't careful it coerces your numbers to string and does a string compare (ie lexicographic ordering) instead of a numeric compare.

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

PATTERN { code }

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 as

BEGIN {minc  =1000}
      {minlo =1000}
      {minla =1000}

So, before the first line of input is processed, minc is set to 1000. Following that, on every input line, minlo and minla are set to 1000. My versions, since they all do the initialization in a BEGIN line, just initialize the values once.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文