user1614417
user1614417

Reputation: 35

extract values from a text file with awk

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

Answers (3)

Lars Kotthoff
Lars Kotthoff

Reputation: 109232

awk '{ if($2 > 20) { print FILENAME " " $1 } }' <files>

Upvotes: 2

potong
potong

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

Vijay
Vijay

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

Related Questions