我想根据 column2 的值从文本文件中提取 column1。只有当 column2 大于 20 时,我才需要打印 column1。我还需要在输出中打印文件名。我怎样才能用 awk 做到这一点?
file1.txt
alias 23
samson 10
george 24
file2.txt
andrew 12
susan 16
david 25
desired output
file1
alias
george
file2
david
我想根据 column2 的值从文本文件中提取 column1。只有当 column2 大于 20 时,我才需要打印 column1。我还需要在输出中打印文件名。我怎样才能用 awk 做到这一点?
file1.txt
alias 23
samson 10
george 24
file2.txt
andrew 12
susan 16
david 25
desired output
file1
alias
george
file2
david
awk '{ if($2 > 20) { print FILENAME " " $1 } }' <files>
这可能对您有用:
awk '$2>20{print $1}' file1 file2
如果你想要文件名和更漂亮的打印:
awk 'FNR==1{print FILENAME} $2>20{print " ",$1}' file1 file2
awk '$2>20{if(file!=FILENAME){print FILENAME;file=FILENAME}print}' file1 file2
见下文:
> awk '$2>20{if(file!=FILENAME){print FILENAME;file=FILENAME}print}' file1 file2
file1
alias 23
george 24
file2
david 25