LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-01-2010, 12:17 PM   #1
dayamoon
LQ Newbie
 
Registered: Apr 2010
Posts: 12

Rep: Reputation: 0
Exclamation merge 2 files with AWK by the field value


Hello,
i have 2 file and want to merge them in the first one.
the first file is like this:
2 word1
1 word2
6 word3
2 word4
......
the second:
word1 :file1 :file2
word2 :file6
word3 :file1 :file2 :file2 :..up to file6
word4 :file7 :file1
.......
So i want to ADD all those File fiels Wich have different FN for every record.
Can anybody helpwith this??
Thank you in advance!!
 
Old 06-01-2010, 12:24 PM   #2
cpplinux
Member
 
Registered: Dec 2009
Posts: 37

Rep: Reputation: 17
awk '{print $1}' 1st_file > 3rd_file
paste 3rd_file 2nd_file > final_file
 
1 members found this post helpful.
Old 06-01-2010, 12:39 PM   #3
dayamoon
LQ Newbie
 
Registered: Apr 2010
Posts: 12

Original Poster
Rep: Reputation: 0
That was great! very simple and easy...
i just have 1 little bug.
in the 2nd file...i have some records that don't match any of 2nd field,
how do i delete them for not to come in the resulting file?
 
Old 06-01-2010, 07:18 PM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
I generally find that if you are asking a question where you have an idea about a before and after picture that you should present both sides.
You have given the before but no after (ie what you want the final file to look like).
Also, prior to asking questions you need to identify such things as your exception as there could be more than one so more back & forward may be required.

Assuming no more changes are required, this may work:
Code:
awk 'FILENAME==ARGV[1]{_[$2]=$1}FILENAME==ARGV[2] && $1 in _{print _[$1],$0 > "new_file"}' file1 file2
 
Old 06-02-2010, 01:42 PM   #5
dayamoon
LQ Newbie
 
Registered: Apr 2010
Posts: 12

Original Poster
Rep: Reputation: 0
The upper AWK code is little unclear to me.. i mean when writing FILENAME u mean file1, file2 or new_file..
The file1 and file2 are not 1:1 exactly that's why i can't do the paste..(it does pasting but not in the correct way) the result file has to be this way:

if[ $2(file1)==$1(file2) ]
write in new_file print $1(file1) $2(file1 or file2 doesnt matter they are same) and i<NF all the other remaining fiels from the 2nd file.. i mean the result should be like this:

2 absolute Filename1 filename2
1 bahami Filname1
3 cucumber filename3 filename1 filename6
 
Old 06-02-2010, 08:57 PM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
I kept everything in the same order that you presented it.
File1:
Code:
2 word1
1 word2
6 word3
2 word4
......
file2:
Code:
word1 :file1 :file2
word2 :file6
word3 :file1 :file2 :file2 :..up to file6
word4 :file7 :file1
.......
new_file is the one created with the two files above combined as requested.
 
Old 06-03-2010, 12:16 AM   #7
dayamoon
LQ Newbie
 
Registered: Apr 2010
Posts: 12

Original Poster
Rep: Reputation: 0
Thak you !!!
that Worked Just exactly as Required!!
ok n i got that the FILENAME is like NF FS etc etc
 
Old 06-03-2010, 12:30 AM   #8
dayamoon
LQ Newbie
 
Registered: Apr 2010
Posts: 12

Original Poster
Rep: Reputation: 0
Thumbs up Thank you!

Thank You!! i just have to read some and understand the meaning of the underscore!!
 
Old 06-03-2010, 02:06 AM   #9
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Nothing special there ... just the name of the array. Same as - array[1], here name is array an there the name was _
 
  


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
[SOLVED] awk: how to print a field when field position is unknown? elfoozo Programming 12 08-18-2010 03:52 AM
awk printing from Nth field to last field sebelk Programming 2 01-08-2010 09:39 AM
awk merge and sum lines problem lalo4080 Programming 4 08-12-2008 10:21 AM
awk command to merge columns from two separate files into single file? johnpaulodonnell Linux - Newbie 4 01-23-2007 10:10 AM
Label merge prints field names, not data RetiredInMaine Linux - Desktop 2 09-30-2006 12:21 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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

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