LinuxQuestions.org
Register a domain and help support LQ
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, 01: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, 01: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, 01: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, 08:18 PM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,256

Rep: Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686
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, 02: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, 09:57 PM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,256

Rep: Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686
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, 01: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, 01: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, 03:06 AM   #9
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,256

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


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


All times are GMT -5. The time now is 06:39 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration