排序并捕获不期望的值(Shell 脚本)

排序并捕获不期望的值(Shell 脚本)

我有一个 .csv 文件,我期望第三列中只有 YES 和 NO 值:

Sample1|Sample2|YES
Sample1|Sample2|YES
Sample1|Sample2|YES
Sample1|Sample2|YES
Sample1|Sample2|NO
Sample1|Sample2|MAYBE(random*)
Sample1|Sample2|MAYBE(random*)

注意:(random*)代表此处可能存在的随机值。

所以我想要的输出应该是:

Expecting value count: 5
YES = 4
NO = 1

Not expecting value count: 2
MAYBE = 2

如果除了 YES 和 NO 之外没有其他值,我想要的输出应该是:

Expecting value count:
YES = 4
NO = 1
Not expecting value count: 0

答案1

也许是这样的:

tr '(' '|' < file | cut -d'|' -f3 | sort | uniq -c

答案2

这是一个与 AIX 兼容的 awk 解决方案:

BEGIN {
  FS="|";
}
{
  values[$3]++
}
END {
  print "Expecting value count: " values["YES"] + values["NO"];
  print "YES = " values["YES"];
  print "NO = " values["NO"];
  delete values["YES"];
  delete values["NO"];
  for (value in values)
    unexpected += values[value];
  if (unexpected) {
    print "\nNot expecting value count: " unexpected;
    for (value in values)
      print value " = " values[value];
  }
}

将其保存到您想要命名的脚本中。

对于此示例输入:

Sample1|Sample2|YES
Sample1|Sample2|YES
Sample1|Sample2|YES
Sample1|Sample2|YES
Sample1|Sample2|NO
Sample1|Sample2|MAYBE1
Sample1|Sample2|MAYBE2
Sample1|Sample2|MAYBE2

...输出是:

$ awk -f i.awk < input
Expecting value count: 5
YES = 4
NO = 1

Not expecting value count: 3
MAYBE1 = 1
MAYBE2 = 2

...对于此示例输入:

Sample1|Sample2|YES
Sample1|Sample2|NO
Sample1|Sample2|NO

...输出是:

$ awk -f i.awk < input2
Expecting value count: 3
YES = 1
NO = 2
Not expecting value count: 0

答案3

for i in $(cat l.txt  | awk -F "|" '{print $3}' | sort | uniq); do
  grep -c $i l.txt;
  echo $i;
done | sed "N;s/\n/ /g"

l.txt 包含您提到的内容。输出是

2 MABYE(random)
1 NO
4 YES

为了计算预期和意外,您可以将输出保存在文件中,并在上一个命令的输出文件上使用以下命令进行预期。

egrep -i "NO|YES" |
awk '{print $1}' |
tr "\n"  " " |
awk '{print "echo" " " $1 " " "+" " " $2 }' |
sed 's/^/"/g' |
sed 's/$/"/g' |
awk '{print $0"|bc -l"}' |
sed 's/"echo/echo "/g' |
sh

相关内容