How can one join files with seperating data in bash?

Solution 1:

Requires GNU sed:

sed -s '$G' *.txt > all.txt

append a line of 8 dashes and a newline after each file

sed -s '$a--------' *.txt

You can use your sed '$d' with that

Compare to these:

Insert a line of dashes before each file:

sed -s '1i--------' *.txt

Do the same, but without a newline after the dashes:

sed -s '1s/^/--------/' *.txt

Put a line of dashes on the end of the last line of each file:

sed -s '$s/$/--------/' *.txt

Surround each file with curly braces:

sed -s -e '1i{' -e '$a}' *.txt

Solution 2:

As a one-liner with subshells:

( for i in *.txt ; do cat $i ; echo 'separator here' ; done ) >all.txt

Here's what the subshell executes split into script-style lines:

for i in *.txt
do
cat $i
echo 'separator goes here' 
done

In this example the separator acts like a footer; add a header by adding another echo before the cat.