Shravan Yadav Shravan Yadav - 6 months ago 12
Bash Question

Merging files data on unix. Proper merge with update and insert in existing file

I have below files. I want to merge the files based on 1st field of file.

File1

a~1~2~3~4
b~4~6~7~8
c~8~9~10~11


File1 is source file and I have received file2 and file3

File2

a~5~6~3~4
b~4~6~7~8
d~9~11~12~13

File3

a~51~61~31~41
b~41~62~72~82
e~11~12~13~14


So File1 records are updated by latest file we receive. here File3 is latest file. So for record having 1st field a is updated with the values present in File3. Record starting with c has no updates in File2 and File3, so it will remain same. Record e in File3 is new so added to File1. So final content of File1

a~51~61~31~41
c~8~9~10~11
b~41~62~72~82
d~9~11~12~13
e~11~12~13~14


First I am trying to get common records from all file but below command is extracting common records from only 2 files.

awk -F"~" 'NR==FNR {a[$1]=$1; next}$1 in a {print $0}' file1 file2


I am working more on it.

Answer
cat file3 file2 file1 |sort -u -t~ -k1,1
a~51~61~31~41
b~41~62~72~82
c~8~9~10~11
d~9~11~12~13
e~11~12~13~14

Description:

open from file3 to file1 ,then unique and sort them based on first column. Colum delimiter is defined as "~ ".