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?


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 *.

  • 7
    you can do this same thing in any shell like echo '**********************' |cat file - file2 – mikeserv Oct 23 '14 at 12:48
  • thanks. does cat always add a new line between files, implicitly? – Tim Oct 23 '14 at 12:48
  • 1
    @Tim - definitely echo always does. – mikeserv Oct 23 '14 at 12:49
  • 1
    @Tim - doesn't look that way here, but see for yourself - printf 'no newline' | tee file1 >file2; printf 'no newline' | cat file1 - file2 – mikeserv Oct 23 '14 at 12:54
  • 1
    @Tim you can use ...<(echo -n '****') to suppress the newline from echo. cat only shows one when the file has one (at the end). – Anthon Oct 23 '14 at 13:16

Another approach without using cat as found from here,

awk 'FNR==1{print "******"}{print}' file1 file2 | sed '1d'

The final sed 1d pipe is to remove the first line which also will have the delimiter.

However the final sed use could very well be avoided if we use the command as suggested by 1_CR in his comments.

awk 'FNR==1 && NR!=1 {print "******"}{print}' file1 file2
  • 1
    you can avoid the sed by changing awk pattern to FNR == 1 && NR != 1 – iruvar Oct 23 '14 at 18:37
  • Congratulations on reaching 10K! – G-Man Says 'Reinstate Monica' Oct 24 '14 at 19:23
  • @G-Man, thanks a lot. That is so nice of you to congragulate :) – Ramesh Oct 24 '14 at 19:24
  • This allows easy extension to regex of file patterns – Paul Mar 7 at 13:41

You can also split it into multiple commands:

cat file1; echo '------------'; cat file2

And you can pipe or redirect that using a group command or a subshell:

{ cat file1; echo '----------'; cat file2; } | other-command
#                                        ^ final semicolon is part of the syntax!

( cat file1; echo '----------'; cat file2 ) | other-command

Of course, you can write that over multiple lines instead of using semicolons, like normal. And it's easy to extend to another file with another delimiter, etc.


With paste:

$ echo '********************************' | paste -sd$'\n' file1 - file2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.