I have a file that looks like this:
我有一个看起来像这样的文件:
3, abc, x
2, def, y
3, ghi, z
I want to find the highest value in $1
and print all rows that contain this highest value in $1
.
我想在$ 1中找到最高值,并打印包含$ 1最高值的所有行。
sort -t, -k1,1n| tail -n1
would just give one of the rows that contain 3 in $1
, but I need both.
只会给出一个包含3美元的行,但我需要两个。
Any suggestions are appreciated (:
任何建议表示赞赏(:
3 个解决方案
#1
2
I’m not sure if this is the nicest way to get lines while they have the same value with awk, but:
我不确定这是获取线条的最好方法,而它们与awk具有相同的值,但是:
awk 'NR == 1 { t = $1; print } NR > 1 { if (t != $1) { exit; } print }'
which can be combined with sort
as follows:
可以结合如下排序:
sort -t, -k1,1nr | awk 'NR == 1 { t = $1; print } NR > 1 { if (t != $1) { exit; } print }'
There’s also this, but it does unnecessary work:
还有这个,但它做了不必要的工作:
sort -t, -k1,1nr | awk 'NR == 1 { t = $1 } t == $1 { print }'
#2
0
Here is another approach that does not require sorting, but requires two passes over the data.
这是另一种不需要排序的方法,但需要对数据进行两次传递。
max=$(awk -F',' '{if(max < $1) max = $1}END{print max}' Input.txt )
awk -v max=$max -F',' '$1 == max' Input.txt
#3
0
In awk, only one pass over the data:
在awk中,只有一个传递数据:
$ awk -F, '
$1>m { # when new max is found
delete a; m=$1; i=0 # reset all
}
a[1]=="" || $1==m { # if $1 equals max or we're processing the first record
a[++i]=$0 # store the record to a
}
END { # in the end
for(j=1;j<=i;j++)
print a[j] # print a with stored records
}
' file
3, abc, x
3, ghi, z
#1
2
I’m not sure if this is the nicest way to get lines while they have the same value with awk, but:
我不确定这是获取线条的最好方法,而它们与awk具有相同的值,但是:
awk 'NR == 1 { t = $1; print } NR > 1 { if (t != $1) { exit; } print }'
which can be combined with sort
as follows:
可以结合如下排序:
sort -t, -k1,1nr | awk 'NR == 1 { t = $1; print } NR > 1 { if (t != $1) { exit; } print }'
There’s also this, but it does unnecessary work:
还有这个,但它做了不必要的工作:
sort -t, -k1,1nr | awk 'NR == 1 { t = $1 } t == $1 { print }'
#2
0
Here is another approach that does not require sorting, but requires two passes over the data.
这是另一种不需要排序的方法,但需要对数据进行两次传递。
max=$(awk -F',' '{if(max < $1) max = $1}END{print max}' Input.txt )
awk -v max=$max -F',' '$1 == max' Input.txt
#3
0
In awk, only one pass over the data:
在awk中,只有一个传递数据:
$ awk -F, '
$1>m { # when new max is found
delete a; m=$1; i=0 # reset all
}
a[1]=="" || $1==m { # if $1 equals max or we're processing the first record
a[++i]=$0 # store the record to a
}
END { # in the end
for(j=1;j<=i;j++)
print a[j] # print a with stored records
}
' file
3, abc, x
3, ghi, z