简体   繁体   English

检查列值的顺序是相同的awk / bash

[英]Checking column values are in the same order awk/bash

I have an input file: 我有一个输入文件:

A 23
A 45
A 32
A 61
A 78
B 23
B 45
B 32
B 61
B 78
C 23
**C 32
C 45**
C 61
C 78

The first column specifies a group, and the second column specifies some values for that group. 第一列指定一个组,第二列指定该组的一些值。

I want to check if the order of values for each group is the same. 我想检查每个组的值顺序是否相同。

For example, the values are 23,45,32,61,78 for group A. The values are in the same order for group B. But in group C there is a violation of the order (in bold), therefore the output should simply be "false". 例如,组A的值是23、45、32、61、78。组B的值是相同的顺序。但是在组C中,该顺序是违反的(粗体),因此输出应为简单地是“假”。

If group C, the order of values is followed as well, the output would be "true". 如果是C组,则也要遵循值的顺序,输出将为“ true”。

Note that the groups in the first column are all unique, there is no repeated group. 请注意,第一列中的组都是唯一的,没有重复的组。

!f { a[++i] = $2 }              # save first sequence
NR>1 && $1!=p { f=1; i=1 }      # set flag and reset index on new sequence
{ p=$1 }                        # save value of first column
f && a[i++]!=$2 { m=1; exit }   # if number not in sequence, set m and exit early
END { print m?"false":"true" }  # print false or true, depending on m

Run it like awk -f script.awk file awk -f script.awk file一样运行

This awk should work: 这个awk应该工作:

awk '$1 != p {
   if (!grp1 && grp)
      grp1 = grp;
  if (grp != grp1) {
     print p " - false";
     done = 1;
     exit
  };
  grp = "";
  p = $1
}
{
   grp = grp ":" $2
}
END {
  if (!done && grp != grp1)
     print p " - false"
}' file

It prints: 它打印:

C - false

Another solution: 另一个解决方案:

 NR == 1 {
    initial = $1
    returnVal = ""
}
{
    if (initial == $1) {
        array[++i] = $2
    } else {
        if (anchor != $1)
            ix = 0
        anchor = $1
        if (array[++ix] != $2) {
            returnVal = 1
        }
    }
}
END {
    print (returnVal) ? "false" : "true" ;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 融合第一列的值,当第二列的值在 awk 或 bash 的不同文件中相同时,使用 awk 或 bash - Fuse values from first column, when the values from second column is the same in different files in awk or bash by using awk or bash 重击:用另一列替换列并使用AWK打印特定顺序 - Bash: replacing a column by another and using AWK to print specific order 使用 AWK 比较同一列中的值 - Using AWK to compare values in the same column 如果所有值都相同,则循环遍历 bash 测试中的列 - AWK - Loop over columns in bash testing if all values are the same - AWK 使用bash,sed和awk增加文件中列的值 - increment values in column within file with bash, sed and awk Bash Awk,记录的相反顺序 - Bash Awk, Reverse order of records Select 每列的内容以精确的顺序排列,同时尊重每行一列(CSV / AWK / BASH) - Select content of each column in a precise order while respecting one column per line (CSV / AWK / BASH) 什么定义了bash中的“列”? 在awk? - What defines a “column” in bash? In awk? awk / bash删除具有唯一ID的行,并将具有最大值/最小值的行保留在同一ID下的列中 - awk/bash remove lines with an unique id and keep the lines that has the max/min value in a column under the same ID Bash:在保留行顺序的同时替换列的值。 - Bash: Replace values of a column while retaining line order.
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM