user175386049
user175386049

Reputation: 499

How to compare two columns in multiple files in linux with awk

I have this code

[motaro@Cyrax ]$ awk '{print $1}' awk1.txt awk2.txt
line1a
line2a
file1a
file2a

It shows the ccolumns from the both files

How can i find $1(of file 1) and $1(of file2) , separately

Upvotes: 2

Views: 2345

Answers (3)

Steve
Steve

Reputation: 54392

As per the comments above, for three or more files, set the conditionals like:

FILENAME == ARGV[1]

For example:

awk 'FILENAME == ARGV[1] { print $1 } FILENAME == ARGV[2] { print $1 } FILENAME == ARGV[3] { print $1 }' file1.txt file2.txt file3.txt

Alternatively, if you have a glob of files:

Change the conditionals to:

FILENAME == "file1.txt"

For example:

awk 'FILENAME == "file1.txt" { print $1 } FILENAME == "file2.txt" { print $1 } FILENAME == "file3.txt" { print $1 }' *.txt

You may also want to read more about the variables ARGC and ARGV. Please let me know if anything requires more explanation. Cheers.

Upvotes: 1

mgamba
mgamba

Reputation: 1199

awk 'NR==FNR{a[FNR]=$0;next} {print a[FNR],$0}' file_1 file_2

found here

Upvotes: 0

Vijay
Vijay

Reputation: 67221

I am not sure exactly what you need. Probably you need predefined variable :FILENAME

awk '{print $1,FILENAME}' awk1.txt awk2.txt

This above command will output:

line1a awk1.txt 
line2a awk1.txt 
file1a awk2.txt 
file2a awk2.txt

Upvotes: 0

Related Questions