Combine two text files with adding some separator between

text processing

cat file1 file2 will combine two text files. But if I want to add some separator between, like a line or two of ********************************, do I have to open the first file, and add the line at its end, or open the second file and add the line at its top, and then run the cat command? Can it be done with just running a command?

Best Answer

In bash and zsh you can do:

cat file1 <(echo '********************************') file2

or as mikeserv indicated in his comment (in any shell):

echo '********************************' | cat file1 - file2

and in Bash as David Z commented:

cat file1 - file2 <<< '********************************'

Any newlines in the files will be shown. If you don't want a newline after the "separator" (e.g. in case file2 starts with a newline) you can use echo -n '****', so suppress the newline after the *.

Related Question