19

I want to cat all the files in a directory, but include some spacer between each one.

5 Answers 5

21

use awk

awk 'FNR==1{print ""}{print}' file* > out.txt
7
  • Nasty recursion. But this works: awk 'FNR==1{print ""}{print}' *.txt > /tmp/out.txt
    – Matt
    Oct 31, 2009 at 1:46
  • what do you mean nasty recursion.?
    – ghostdog74
    Oct 31, 2009 at 1:55
  • 1
    If I run that line alone the command never quits, and keeps appending the files to the out.txt (which is in the same folder of the files I'm cat-ing. Out.txt gets very large, very fast. (Again, OSX 10.5.8 YRMV).
    – Matt
    Oct 31, 2009 at 1:58
  • file* in my example means processing files with file names starting with file. therefore if you have names like file1, file2, file3 then it will process. The only difference now is you change to *.txt. I could only guess that you have MANY files with names starting with file?? If not, I don't see why it will into recursion and never quits, as it works for me. Anyway, since there's a work around for you, then should be fine.
    – ghostdog74
    Oct 31, 2009 at 2:04
  • 2
    awk 'FNR==1{}{print}' file* > out.txt worked better for me. Thanks.
    – rahul286
    Jun 12, 2014 at 11:38
13

Try

find . -type f -exec cat {} \; -exec echo "-- spacer --" \;

Obviously, the 'spacer' can be more than the simple example used here.

4
  • Works, but I can't use "\n" as my spacer, it just uses a literal '\n'?
    – Matt
    Oct 31, 2009 at 2:21
  • Just use echo "" for a blank line. I should have said the spacer could be more or less than the example.
    – pavium
    Oct 31, 2009 at 2:29
  • sounds like you have a different implementation than mine. How about echo '' or just echo ?
    – pavium
    Oct 31, 2009 at 2:33
  • The -printf "\n" "action" is also viable; I believe it also avoids invoking echo or any other subprocess. Dec 4, 2016 at 8:44
2

You might want to see pr(1), which may do what you want out-of-the-box.

To roll your own, expand this posix shell script fragment:

ls -1  | while read f; do cat "$f"; echo This is a spacer line; done > /tmp/outputfile

This might more readably be written as:

ls -1 | while read f; do
    cat "$f"
    echo This is a spacer line
done > /tmp/outputfile

You don't really need the -1 for ls.

2
  • Thanks! Just remember to redirect the output to another directory (as in the awk solution).
    – Matt
    Oct 31, 2009 at 1:55
  • You don't need the ls at all. Just for f in *; do cat ... (porkmail.org/era/unix/award.html#ls)
    – Jacktose
    Mar 19, 2019 at 20:18
-1

I think the simplest way is using the paste command:

paste $(ls *) > file.out
1
  • Um, no, this just doesn't work. Paste does something similar but different to what the question asked. It interlaces the files.
    – Caleb
    Dec 10, 2016 at 14:07
-2

echo "" > blank.txt
cat f1.txt blank.txt f2.txt blank.txt f3.txt

To handle all of the files in a Directory (assuming ksh like Shell)

for file in * ; do
   cat $file >> result.txt
   echo "" >> result.txt
done

2
  • That doesn't take care of all files in the directory unless you manually list them as parameters. Oct 31, 2009 at 1:29
  • be careful if you cat like that with a for loop expanding everything. $file could be a directory and you will be catting a directory.
    – ghostdog74
    Oct 31, 2009 at 1:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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