LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   merge 2 files with AWK by the field value (https://www.linuxquestions.org/questions/linux-newbie-8/merge-2-files-with-awk-by-the-field-value-811523/)

dayamoon 06-01-2010 12:17 PM

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!!

cpplinux 06-01-2010 12:24 PM

awk '{print $1}' 1st_file > 3rd_file
paste 3rd_file 2nd_file > final_file

dayamoon 06-01-2010 12:39 PM

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?

grail 06-01-2010 07:18 PM

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

dayamoon 06-02-2010 01:42 PM

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

grail 06-02-2010 08:57 PM

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.

dayamoon 06-03-2010 12:16 AM

Thak you !!!
that Worked Just exactly as Required!!
ok n i got that the FILENAME is like NF FS etc etc

dayamoon 06-03-2010 12:30 AM

Thank you!
 
Thank You!! i just have to read some and understand the meaning of the underscore!!

grail 06-03-2010 02:06 AM

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


All times are GMT -5. The time now is 10:05 PM.