merge two file

how to merge two file, i have 2 input file; file_1 & file_2, so i want the output file_3 as below

    file_1

NAME
aa
111
222
bb.txt
111
222
cc
111
111
dd
222
ee
111
222

file_2
NAME
/usr/my_work/aa
/usr/my_work/bb
/usr/my_work/cc
/usr/my_work/dd
/usr/my_work/ee

output file

    file_3

/usr/my_work/aa
111
222
/usr/my_work/bb
111
222
/usr/my_work/cc
111
111
/usr/my_work/dd
222
/usr/my_work/ee
111
222

awk -F"[. ]" 'NR==FNR{f=$0;sub(".*/","",f);F[f]=$0;next} $1 in F{$0=F[$1]} !/NAME/' file_2 file_1

its worked..thanks, can u explain the code

Uses dot and space as field separators, this is so we can pickup filename without extension (eg bb from bb.txt).

NR==FNR{f=$0;sub(".*/","",f);F[f]=$0;next} Store pathnames in file_2 into array F[] with index as basename of file.

$1 in F{$0=F[$1]} if field 1 (bit before any fullstop) from file_1 is available in the F[] array then assign line as the fullpathname we stored from file_1 earlier, otherwise leave it as it is.

!/NAME/ If the new line from previous command does not contain NAME then print it out.[/icode]

1 Like

thanks bro