Search code examples
linuxblank-line

Insert empty line after the contents of a file in a new file


This is a simple problem, I'm just stuck on it. I am taking the contents of a bunch of different files and printing each file's name as a header before its contents. That much works. But I want to have an empty line separating the the contents of one file and the header for the next file's content.

I want it to look like:

File 1 header

File 1 contents

[empty space]

File 2 header

File 2 contents

I tried putting \n after "{}" in my code, but that didn't work. Any suggestions?

find . -type f -name '*_top_hits.txt' -print -exec cat {} \; > combinedresults.txt

Solution

  • You can just an empty echo as part of the find -exec as

    find . -type f -name "*_top_hits.txt" -print -exec sh -c "cat {};echo"  \; > combinedresults.txt
    

    The echo just produces a single empty new-line after each file content. Also you don't need multiple -exec options rather use a single sub-shell.