Alex Rivera | Logout

Unix command to prepend text to a file

Asked 2012-05-14T16:45:52.193
219

Is there a Unix command to prepend some string data to a text file?

Something like:

prepend "to be prepended" text.txt
Edit
Report

1 Answer

24

This will work to form the output. The - means standard input, which is provide via the pipe from echo.

echo -e "to be prepended \n another line" | cat - text.txt

To rewrite the file a temporary file is required as cannot pipe back into the input file.

echo "to be prepended" | cat - text.txt > text.txt.tmp
mv text.txt.tmp text.txt
answered 2012-05-14T16:52:06.423

Your Answer