I have a file with two columns as
1 1 2 3 3 4
and a file with one column as
6 7 9
I would like to add the second file in the first one. The output should be:
1 1 6 2 3 7 3 4 9
For reference, paste -d' ' file1 file2 will paste file2 to file 1 (as an appended column) with a space as a delimiter; paste -d'\t' file1 file2 will paste with a tab as a delimiter. E.g. paste -d'\t' file1. tsv file2.
ALT + Left Mouse Click puts you in Column Mode Select. It's quite an useful shortcut that may help you. This is by far the simplest solution.
$ pr -mts' ' file1 file2 1 1 6 2 3 7 3 4 9 $ paste -d' ' file1 file2 1 1 6 2 3 7 3 4 9
awk 'NR==FNR{a[NR]=$0;next}{print a[FNR],$0}' file1 file2
Note: Will work with files of same length. If file lengths' are different, go with sudo_O's solution.
Just for the heck of it, here is an awk
command that I think should simulate paste
. Purely for fun though, if I were you I would still go with sudo_O's solution (or may be not!)
awk 'NR==FNR{a[++y]=$0;next}{b[++x]=$0} END{z=x>y?x:y;while(++i<=z){print a[i],b[i]}}' file1 file2
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With