How can I concatenate a file and string into a new file in UNIX as a one line command

Christian picture Christian · Jul 9, 2012 · Viewed 18.3k times · Source

I need a very simple (hopefully) 1 liner command that reads in a file appends a string and outputs to a new file, without changing the original data.

file1               string
------              ------
apple               oranges
bananas

MAGIC COMMAND

filel               file2
------              ------
apple               apple
bananas             bananas
                    oranges

basically, cat file1 + 'oranges' > file2

I'm using autosys to run the command, and I'm pretty sure it doesn't allow for && or ; as part of a single command.

Answer

nos picture nos · Jul 9, 2012

You can do this:

(cat file1 ; echo 'oranges') > file2

Which will spawn one subshell, which will cat file1 to stdout, then echo oranges to stdout. And we capture all that output and redirect it to a new file, file2.

Or these 2 commands:

cp file1 file2 
echo 'oranges' >> file2

Which first copies file1 to the new file2, and then appends the word oranges to file2

Here's another one liner that does not use ; nor &&

echo oranges | cat file1 - > file2