Reputation: 35
I would like to extract column1 from the text files based on the values of column2. I need to print column1 only if the column2 is greater than 20.I also need to print the name of the file with the output. How can I do this with awk?
file1.txt
alias 23
samson 10
george 24
file2.txt
andrew 12
susan 16
david 25
desired output
file1
alias
george
file2
david
Upvotes: 1
Views: 274
Reputation: 58371
This might work for you:
awk '$2>20{print $1}' file1 file2
if you want file names and prettier printing:
awk 'FNR==1{print FILENAME} $2>20{print " ",$1}' file1 file2
Upvotes: 1
Reputation: 67211
awk '$2>20{if(file!=FILENAME){print FILENAME;file=FILENAME}print}' file1 file2
see below:
> awk '$2>20{if(file!=FILENAME){print FILENAME;file=FILENAME}print}' file1 file2
file1
alias 23
george 24
file2
david 25
Upvotes: 0