Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combining echo and cat on Unix

Tags:

shell

unix

piping

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.

like image 796
Dan Avatar asked Jun 09 '10 11:06

Dan


People also ask

What is echo $? In Unix?

echo $? returns the return value (exit status) of the last executed command (0 is usually success ).

What is the difference between echo cat and cat echo command in Linux?

You can also use echo to to redirect the standard output of the command on the left and append it to the end of the file on the right. The output will be added to the file hello. txt. Whereas the command cat displays the contents of one or more files to the terminal.

Is echo a Unix command?

Echo is a Unix/Linux command tool used for displaying lines of text or string which are passed as arguments on the command line. This is one of the basic command in linux and most commonly used in shell scripts. In this tutorial, we will look at the different options of echo command.


1 Answers

This should work:

echo "PREPENDED STRING" | cat - /tmp/file | sed 's/test/test2/g' > /tmp/result  
like image 123
Douglas Avatar answered Sep 25 '22 08:09

Douglas