frazras
frazras

Reputation: 6488

Append text to a piped file

I am piping a file through bash how could I append some text to the end of it?

cat filename.txt | append "text to append" | final_command

EDIT: This has to be done without creating a new file

Upvotes: 1

Views: 742

Answers (3)

Lungang Fang
Lungang Fang

Reputation: 1537

In my opinion, this solution is more concise:

echo "text to append" | cat filename.txt - | final_command

It is also more flexible, for instance, you can

echo "text to append" | cat file1.txt - file2.txt ... | final_command

The solution which OP chose would be awkward in the second case.

Upvotes: 0

o11c
o11c

Reputation: 16056

A couple more alternatives:

cat filename.txt <(echo "text to append") | final_command

final_command <(cat filename.txt; echo "text to append")

(assuming final_command can take input from an argument instead of the default stdin)

Upvotes: 1

Roman
Roman

Reputation: 6656

This should do it:

(cat filename.txt && echo "text to append") | final_command

If you don't want a newline character at the end, use echo -n:

(cat filename.txt && echo -n "text to append") | final_command

Upvotes: 5

Related Questions