计算最小值时排除awk中的字符



下面是我拥有的数据集

10.25 3.5
10.15 4.5
# value1 
10.25 3.5
12.15 4.5
11.25 3.5
10.05 4.5

我试着用找到第1列的最小值

awk 'NF' FILENAME |sort -k1 - | awk 'NR==1{print $1}' 

但我忍受

#

结果。我也试过使用-F"|#"。。但是结果是一样的。我的预期输出是列1 的最小值

10.05

我有数据文件的列数字和特殊字符。我现在想从每个文件中获取Min

所需输出

10.05(from file1)
9.02 (from file2)
..... (from n files)

我的问题是,如何忽略包含此类字符的行,并从每个文件中获取所有最小值?

感谢

您可以使用此awk:

awk 'FNR==1 {
if (min != "")
print min, "(from", fn ")"
min = ""
fn = FILENAME
}
$1+0 == $1 && (min=="" || $1 < min) {
min=$1
}
END {
print min, "(from", fn ")"
}' *.txt

10.05 (from file1.txt)
9.02 (from file2.txt)
  • $1+0 == $1检查是为了确保$1是一个数字字段
  • (min=="" || $1 < min):如果未设置min或如果$1 < min,则设置min=$1
awk '($1+0) == $1{print $1, "(from " FILENAME ")"}' file* | sort -k2 -k1,1n | uniq -f1

例如:

$ cat file1
10.25 3.5
10.15 4.5
# value1
10.25 3.5
12.15 4.5
11.25 3.5
10.05 4.5
$ cat file2
10.25 3.5
10.15 4.5
# value1
10.25 3.5
12.15 4.5
11.25 3.5
10.05 4.5
$ cat file3
10.25 3.5
10.15 4.5
# value1
10.25 3.5
12.15 4.5
11.25 3.5
10.05 4.5

$ awk '($1+0) == $1{print $1, "(from " FILENAME ")"}' file{1..3}
10.25 (from file1)
10.15 (from file1)
10.25 (from file1)
12.15 (from file1)
11.25 (from file1)
10.05 (from file1)
10.25 (from file2)
10.15 (from file2)
10.25 (from file2)
12.15 (from file2)
11.25 (from file2)
10.05 (from file2)
10.25 (from file3)
10.15 (from file3)
10.25 (from file3)
12.15 (from file3)
11.25 (from file3)
10.05 (from file3)

$ awk '($1+0) == $1{print $1, "(from " FILENAME ")"}' file{1..3} | sort -k2 -k1,1n
10.05 (from file1)
10.15 (from file1)
10.25 (from file1)
10.25 (from file1)
11.25 (from file1)
12.15 (from file1)
10.05 (from file2)
10.15 (from file2)
10.25 (from file2)
10.25 (from file2)
11.25 (from file2)
12.15 (from file2)
10.05 (from file3)
10.15 (from file3)
10.25 (from file3)
10.25 (from file3)
11.25 (from file3)
12.15 (from file3)

$ awk '($1+0) == $1{print $1, "(from " FILENAME ")"}' file{1..3} | sort -k2 -k1,1n | uniq -f1
10.05 (from file1)
10.05 (from file2)
10.05 (from file3)

最新更新