Concatenate two files and separate them with a newline
I have two files:
k.txt:
3 5 7 9 19 20
h.txt:
000010
100001
111001
if I just use cat, there is no newline. I need a command which would provide a file which looks like this:
3 5 7 9 19 20
000010
100001
111001
If, as steeldriver suggests, your files don't end with a newline, you could try:
awk '{print}' k.txt h.txt > newfile
or, if you like golfing
awk 1 k.txt h.txt > newfile
or
perl -lne 'print' k.txt h.txt
or
( cat k.txt ; echo ""; cat h.txt; echo ) > newfile
or
echo "$(cat k.txt)"; echo "$(cat h.txt)"
Try this with bash:
cat k.txt <(echo) h.txt > new.txt
Using sed
:
sed '/^/ r h.txt' k.txt
or better (thx @steeldriver)
sed '$a\' k.txt h.txt
Using ed
:
(echo "0a"; cat k.txt; echo "."; echo "wq") | ed -s h.txt
and for the missing newline in k.txt
:
(echo "0a"; cat k.txt; echo ""; echo "."; echo "wq") | ed -s h.txt
or if you need a separate output file:
(echo "0a"; cat k.txt; echo ""; echo "."; echo "w out.txt"; echo "q") | ed -s h.txt