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!! |
awk '{print $1}' 1st_file > 3rd_file
paste 3rd_file 2nd_file > final_file |
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? |
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 |
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 |
I kept everything in the same order that you presented it.
File1: Code:
2 word1 Code:
word1 :file1 :file2 |
Thak you !!!
that Worked Just exactly as Required!! ok n i got that the FILENAME is like NF FS etc etc |
Thank you!
Thank You!! i just have to read some and understand the meaning of the underscore!!
|
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. |