Quantcast
Viewing latest article 16
Browse Latest Browse All 31

Answer by ozzy for how can I merge two text files together

If file1 is your first text file, and file2 is your second text file, this should work in Bash:

join -a 1 <(sort -k1,1 file2) <(sed -E -e 's/([^\s])\(/\1 (/' file1 | sort -k1,1)

Join is called with two files as arguments, which two files (anonymous pipes or FIFOs actually) are the result of two process substitutions that take the form <(...)in the above command.

The second argument file contains a modified version of file1, having a space inserted before the first ( on each line so as to create a proper space-delimited join field; this modification is done by sed. The contents of both argument files are sorted based on the join field (i.e. field 1), as required by join. The -a 1 option ensures that non-pairable lines in file2 (i.e. the first argument file) are also output.


Viewing latest article 16
Browse Latest Browse All 31

Trending Articles