LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices

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

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


Hi, folks...

i have two files;

file1;

xcvfdertyu
asdcvfgtre
asdfghnbvc
werdfcvbgf

file2;
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
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,508

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
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:
Code:
FNR == NR {
  _[$1]++
}

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:
Code:
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.
 
  


Reply


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


All times are GMT -5. The time now is 04:15 PM.

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