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

3

11

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.

Lavaliere answered 9/7, 2012 at 19:15 Comment(5)
What's wrong with the code you provided?Fraser
cat thinks th + and 'oranges' are files and says it can't open them.Lavaliere
Can't you just cat file1 into file2 and then append the string? You can even execute both commands in a single line.Suspiration
That's what I want to do, but I haven't figured out how. I've tried all sorts of variations on cat file1 > echo $string >> file2 but nothing works. So when you say "can't I?" I say, I'm sure I can, but I haven't figured out the actual command.Lavaliere
I came up with this: cp file1 file2 && echo 'oranges' >> file2 but I'm using autosys to run the command, and I'm almost certain it won't let me string commands together with &&Lavaliere
L
35

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
Lw answered 9/7, 2012 at 19:33 Comment(7)
Yeah, I had come up with that one too, but because I'm using Autosys to run the command, I don't think I can include semi-colons or && in the command.Lavaliere
@Christian: Do they really need to be a single line? Can't you put the two commands in a shell script and run it?Suspiration
@Lavaliere Better figure out what kind of commands you can run in autosys. Or use the last approach here, which runs 2 consecutive commands.Lw
@tudor: I could very well create a shell script and do it. But thanks to work, it adds more work to get things into different environments, so a one liner would be best.Lavaliere
@nos: Can't do && and can't do ; in the command. Pipe and std_out are allowed thoughLavaliere
@Lavaliere Then use the last one-liner I just edited. If that's no good, your task would already be finished if you did write a small shell script to overcome the limitations of autosysLw
@nos: Writing the shell script is easy. Using the script as a solution at my workplace adds extra paperwork that I want to avoid. ;)Lavaliere
P
1

Then this should do it:

$cat file1 >> file2; echo "orange" >> file2;
Pocketful answered 9/7, 2012 at 19:34 Comment(1)
No, first file should remain unchanged.Finnougrian
C
1
awk '1; END{ print "oranges"}' file1 > file2

You can probably use the standard solution (given by nos) if you pass it as a string to sh:

sh -c 'cat file1; echo oranges' > file2
Cockeye answered 10/7, 2012 at 10:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.