godzsa
godzsa

Reputation: 2395

How to concatenate two files and write between them?

I am trying to achieve something like this with bash script:

c.txt:

contents of a.txt
###
contents of b.txt

Basically I want to write a constant string between the contents of two files and save to a new one without modifying the originals.

This was the closest I could get:

echo "###" >> a.txt|cat b.txt >> out.txt

Upvotes: 0

Views: 99

Answers (2)

davir
davir

Reputation: 942

You could do it with three commands:

cat a.txt > out.txt
echo "###" >> out.txt
cat b.txt >> out.txt

And perhaps make a function out of it:

append_hash() { cat $1 > $3; echo "###" >> $3; cat $2 >> $3; }

Usage:

append_hash a.txt b.txt out.txt

Upvotes: 3

Tom Zych
Tom Zych

Reputation: 13576

Using - as a filename usually means to use standard input. Thus:

echo 'something' | cat a.txt - b.txt > new.txt

Upvotes: 8

Related Questions