LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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 03-20-2013, 02:18 AM   #1
phpshell
Member
 
Registered: Nov 2012
Posts: 46

Rep: Reputation: Disabled
Question help on awk print matched field


I am really need help
I have two files

file1
frank 101 4544444 glass
fahad 102 4547977 car


file2
101 transfer 888
105 transfer 999

output
101 transfer 888

i am using
awk 'NR==FNR{a[$2];next} ($1 in a)' file1 file2


anybody help me to print as below
frank 101 transfer 888
 
Old 03-20-2013, 02:51 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,842

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
Seeking your initiative to follow the pattern in the script and do it yourself.

do you know how that awk script works? In that case you can find easily the way.....

awk ' NR==FNR { a[$2]=$4; next } ($1 in a) { print a[$1]$0 } ' file1 file2
 
1 members found this post helpful.
Old 03-20-2013, 03:01 AM   #3
phpshell
Member
 
Registered: Nov 2012
Posts: 46

Original Poster
Rep: Reputation: Disabled
Dear pan64
many thanks for your quick respond
i just understand the pattern right now because i am new on this
 
Old 03-20-2013, 03:48 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,842

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
if you really want to say thanks just click on YES (bottom right corner)
 
Old 03-20-2013, 07:34 AM   #5
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by pan64 View Post
Code:
awk ' NR==FNR { a[$2]=$4; next } ($1 in a) { print a[$1]$0 } ' file1 file2
Using this code I obtained this unexpected result:
Code:
glass101 transfer 888
Please advise.

Daniel B. Martin
 
Old 03-20-2013, 07:54 AM   #6
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,842

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
yes, and it is your job to fix it
 
Old 03-20-2013, 08:09 AM   #7
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
The problem statement calls for matching two files on a key value.
The linux command join is suitable for this task.

InFile1 ...
Code:
frank 101 4544444 glass
fahad 102 4547977 car
herman 103 454212 clock
charles 107 454822 television
alfred 115 454629 radio
david 117 454133 table
george 122 454009 desk
InFile2 ...
Code:
101 transfer 888
105 transfer 999
106 sold 123
111 stolen 345
115 destroyed 234
122 missing 666
This code ...
Code:
join -1 1 -2 2  $InFile2 $InFile1 |cut -d" " -f1-3 >$OutFile
... produced this OutFile ...
Code:
101 transfer 888
115 destroyed 234
122 missing 666
The sample input files were sorted on the key value.
This proposed solution assumes this will always be the case.

Daniel B. Martin
 
Old 03-20-2013, 09:19 AM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
The awk solution, which obviously needs a little tweak ( I believe pan64 is hinting the direction without giving the full solution, which I like as it is clear the OP has not done enough investigation
yet), does not require the data to be sorted just that the value of the array index is unique.
 
Old 03-20-2013, 10:02 AM   #9
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by grail View Post
The awk solution ... does not require the data to be sorted just that the value of the array index is unique.
The short sample input files provided by OP have the key fields in sorted order. He did not explicitly state that his files are already sorted, and we cannot know the details of his application. If those files are already sorted there may be a performance advantage in using join.

I don't know the internals of awk, but technical intuition suggests that the ($1 in a) part of
Code:
awk ' NR==FNR { a[$2]=$4; next } ($1 in a) { print a[$1]$0 } ' file1 file2
results in a serial search. If the input files are large, a serial search can be painfully slow.

Daniel B. Martin
 
Old 03-20-2013, 10:14 AM   #10
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,842

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
here you can find an interesting (but old) discussion: http://awk.info/?news/mawkHashing
and here some other info: http://www.cse.yorku.ca/~oz/hash.html

if file1 sorted there is a much better solution of course
 
Old 03-20-2013, 10:46 AM   #11
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
hmmm ... not sure on the serial search idea (is an indexed array using numbers a serial search when you say is N in array?), but a sorted system of course will return faster results
 
  


Reply



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
How to print the field separator in awk? 915086731 Linux - Desktop 5 09-04-2011 09:55 AM
[SOLVED] awk: how to print a field when field position is unknown? elfoozo Programming 12 08-18-2010 03:52 AM
print FS (field separator) in awk wakatana Programming 5 11-05-2009 08:17 AM
How to parameterize which field awk should print? dbland07666 Linux - Newbie 2 10-29-2007 03:49 PM
AWK: print field to end, and character count? ridertech Linux - Newbie 1 05-07-2004 05:07 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:59 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration