How do I include a blank line between files I'm concatenating with “cat”?

前端 未结 5 1742
小鲜肉
小鲜肉 2020-12-16 14:14

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

相关标签:
5条回答
  • 2020-12-16 14:57

    I think the simplest way is using the paste command:

    paste $(ls *) > file.out
    
    0 讨论(0)
  • 2020-12-16 15:01

    Try

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

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

    0 讨论(0)
  • 2020-12-16 15:07

    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.

    0 讨论(0)
  • 2020-12-16 15:08

    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

    0 讨论(0)
  • 2020-12-16 15:17

    use awk

    awk 'FNR==1{print ""}{print}' file* > out.txt
    
    0 讨论(0)
提交回复
热议问题