Latest LQ Deal: Linux Power User Bundle
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 11-17-2011, 03:34 PM   #1
LQ Newbie
Registered: Nov 2011
Posts: 2

Rep: Reputation: Disabled
awk comparing first columns of two files

Hi, folks...

i have two files;



xcvfdertyu dfdsdgfdsgsdgsdfdsfdfsfsdfdsfgsd
asdcvfgtre sdfgsddfsdfdsfdsfsdfdsfgdsfgdsfsd
asdfghnbvc sdfsdffgsdfsdfdsfdsfsdsafdsgrhbfh
werdfcvbgf xxcvcvdssdfeafefaefrertthythmjnhm

I need compare if all columns of file1 exist in file2, and if exist print

first column file1, first and second column file2 and so on

xcvfdertyu xcvfdertyu dfdsdgfdsgsdgsdfdsfdfsfsdfdsfgsd
asdcvfgtre asdcvfgtre sdfgsddfsdfdsfdsfsdfdsfgdsfgdsfsd
asdfghnbvc asdfghnbvc sdfsdffgsdfsdfdsfdsfsdsafdsgrhbfh
werdfcvbgf werdfcvbgf xxcvcvdssdfeafefaefrertthythmjnhm

I tried this:
while (getline < ARGV[1]) {
field1 = $1;
while (getline < ARGV[2]) {
field2 = $1;
field3 = $2;
if (field1==field2) {
print field1, field2, field3; {

but its returns only the first line

Thanks a lot

Ana Paula
Old 11-17-2011, 03:50 PM   #2
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
Indeed two nested while getline loops are not the solution, because after the first loop reads the first line of file1, the second loop immediately reads all the content of file2, so that only the first matching line from file1 is processed.

I would try something like this: first let awk read all the content of file1 and store each line as index of an array; second let awk read the second file and check if $1 is an index of the array:
FNR == NR {

FNR < NR {
  if ( $1 in _ ) print $1, $0
This uses the difference between the internal variables FNR and NR to distinguish between the two files. Run as:
awk 'FNR == NR {_[$1]++} FNR < NR {if ( $1 in _ ) print $1, $0} file1 file2
that is by passing file1 as first argument. Hope this helps.

Last edited by colucix; 11-17-2011 at 03:51 PM.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
extracting columns from multiple files with awk orcaja Linux - Newbie 7 02-14-2012 10:24 PM
[SOLVED] Comparing two fields in two files using Awk. Tauro Linux - Newbie 16 07-21-2011 12:47 AM
[SOLVED] AWK (or TCL/TK): Matching rows and columns between multiple files Euler2 Programming 6 05-30-2011 06:31 PM
[SOLVED] bash script: merge files comparing columns... masavini Programming 18 04-23-2011 05:01 AM
awk command to merge columns from two separate files into single file? johnpaulodonnell Linux - Newbie 4 01-23-2007 10:10 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:03 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration