How to Compare two files and then append line that is not a partial match?
You can parse the files with awk
awk -F '/' '
FNR == NR {seen[$1] = $0; next}
{if ($1 in seen) print seen[$1]; else missing[$1]}
END {for (x in missing) print x}
' Temp.txt Extensions.txt
Output:
1234/sip:[email protected]:5060 9421b96c5e Avail 1.480
4321/sip:[email protected]:5060 e9b6b979a4 Avail 1.855
111
- Set field separator to slash,
-F '/'
- The action after
FNR == NR
is executed for the lines of the first input file. We store the lines in the associative arrayseen
as keys, and go tonext
line. - The second action is executed for the second file, when
FNR != NR
. If the first field matches, we print the stored line,else
we save the field into another arraymissing
. - At the
END
, we print the missing lines.
You could read the contents of Extensions.txt
into an array, delete the partial matches, then print whatever remains:
$ awk -F/ '
NR==FNR {a[$1]; next} {for(i in a) if($1 ~ i) delete a[i]} END{for(i in a) print i} 1
' Extensions.txt Temp.txt
1234/sip:[email protected]:5060 9421b96c5e Avail 1.480
4321/sip:[email protected]:5060 e9b6b979a4 Avail 1.855
111
Using grep
+cut
:
grep -xvFf <(cut -d'/' -f1 tmp) ext >> tmp
Here we are safe in grep
using tmp
for the input in process-substitution as the patterns feed and write the result back into same tmp
file in append mode; see the explanation in below link:
Using same filename for the input in sub-shell and also as output in parent shell will conflict?