输入
a 1 20
4 50
b 5 20
2 90
r 5 70
现在我想从第 3 列及其相应的第 1 列字母中检索 >=50 的信息。
输出
a 4 50
b 2 90
r 5 70
$ cat file
a 1 20
4 50
b 5 20
2 90
r 5 70
$ awk 'BEGIN{FS=OFS="\t"} {if ($1=="") $1=k; else k=$1} $NF>50' file
b 2 90
r 5 70
$ awk 'BEGIN{FS=OFS="\t"} {if ($1=="") $1=k; else k=$1} $NF>=50' file
a 4 50
b 2 90
r 5 70
试试这个单线(awk):
awk -F'\t' -v OFS="\t" '$1!=" "{k=$1}$NF<50{next}$1=k' file
与您的文件:
kent$ cat f
a 1 20
4 50
b 5 20
2 90
r 5 70
kent$ awk -F'\t' -v OFS="\t" '$1!=" "{k=$1}$NF<50{next}$1=k' f
a 4 50
b 2 90
r 5 70
编辑
$1!=" "{k=$1} if $1 is not " ", assign $1 to var k
$NF<50{next} if the last col ($NF) < 50, stop processing, read next line
$1=k otherwise, assign k to $1, and print the line.