如何仅在Unix awk命令返回的列';是数字的吗?

如何仅在Unix awk命令返回的列';是数字的吗?,unix,awk,Unix,Awk,我有一个输入文件,按模式显示如下: hi this is the first line this is the second line ... more lines ... Name Number Position Bob 1 Employee Sam 21 Manager Joe 25 Employee 我想确保数字列中的数字(或数字)是按升序排列的。我正在使用下面的awk命令,但

我有一个输入文件,按模式显示如下:

hi this is the first line
this is the second line 
...
more lines 
... 

    Name Number  Position 
    
    Bob   1      Employee
    
    Sam   21     Manager
    
    Joe   25     Employee
我想确保数字列中的数字(或数字)是按升序排列的。我正在使用下面的awk命令,但它总是将输出作为“OK”,即使顺序不是升序:

awk 'BEGIN {res = "OK"}
     NR > 1 && $2 ~ /[[:digit:]]+/ && $2 <= prev {res = "Fail"; exit}
     {prev = $2}
     END {print res}'  file
awk'开始{res=“OK”}
NR>1&&$2~/[:digit:]+/&$2
awk'BEGIN{res=“OK”}$0~/Name/&&$0~/Number/&&$0~/Position/{proc=1;next}proc==1&&2给定:

我会按照以下思路做一些事情:

$  awk '$2+0!=$2 || NF!=3 {next}
        !flag {prev=$2; flag=1; next}
        prev>$2 {bad=1; exit}
        {prev=$2}
        END{if (bad) print "FAIL"; else print "SUCCESS"}' file
打印
“成功”

现在尝试以下方法:

$ cat file
hi this is the first line
this is the second line 
...
more lines 
... 

    Name Number  Position 
    
    Bob   1      Employee
    
    Sam   21     Manager
    
    Joe   20     Employee

打印
“FAIL”

为什么要使用
$1
而不是
$2
?数字列是第二列,但您引用的是第一列?^^^^此外,在第二行
prev=“number”
。您可能需要特别/单独地处理
NR==2
。对不起,输入错误是$2,我已经更正了为什么您的行之间有空行?您是在Linux或DOS环境下吗?第一行不是我要展示的专栏…前面可能有很多行,我只是展示了一个片段。这是用提供的示例测试的。如果文件中有任何其他信息,请在帖子中发布一个示例。在postOK中完成,我已经修改了解决方案。它总是为我打印成功,有时说不成功found@A.G.Progm.Enthusiast:这里的两个答案都是根据你给出的例子来回答的。显然,您没有完全描述正在使用的文件。
$  awk '$2+0!=$2 || NF!=3 {next}
        !flag {prev=$2; flag=1; next}
        prev>$2 {bad=1; exit}
        {prev=$2}
        END{if (bad) print "FAIL"; else print "SUCCESS"}' file
$ cat file
hi this is the first line
this is the second line 
...
more lines 
... 

    Name Number  Position 
    
    Bob   1      Employee
    
    Sam   21     Manager
    
    Joe   20     Employee