Search code examples
unixshellpiping

Combining echo and cat on Unix


Really simple question, how do I combine echo and cat in the shell, I'm trying to write the contents of a file into another file with a prepended string?

If /tmp/file looks like this:

this is a test

I want to run this:

echo "PREPENDED STRING"
cat /tmp/file | sed 's/test/test2/g' > /tmp/result 

so that /tmp/result looks like this:

PREPENDED STRINGthis is a test2

Thanks.


Solution

  • This should work:

    echo "PREPENDED STRING" | cat - /tmp/file | sed 's/test/test2/g' > /tmp/result