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.

ArjunShankar
  • 23,020
  • 5
  • 61
  • 83
Christian
  • 503
  • 3
  • 7
  • 21
  • What's wrong with the code you provided? – Squazic Jul 09 '12 at 19:18
  • cat thinks th + and 'oranges' are files and says it can't open them. – Christian Jul 09 '12 at 19:23
  • Can't you just cat file1 into file2 and then append the string? You can even execute both commands in a single line. – Tudor Jul 09 '12 at 19:28
  • 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. – Christian Jul 09 '12 at 19:32
  • 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 && – Christian Jul 09 '12 at 19:35

3 Answers3

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
nos
  • 223,662
  • 58
  • 417
  • 506
  • 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. – Christian Jul 09 '12 at 19:36
  • @Christian: Do they really need to be a single line? Can't you put the two commands in a shell script and run it? – Tudor Jul 09 '12 at 19:37
  • @Christian Better figure out what kind of commands you can run in autosys. Or use the last approach here, which runs 2 consecutive commands. – nos Jul 09 '12 at 19:40
  • @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. – Christian Jul 09 '12 at 19:50
  • @nos: Can't do && and can't do ; in the command. Pipe and std_out are allowed though – Christian Jul 09 '12 at 19:50
  • @Christian 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 autosys – nos Jul 09 '12 at 19:57
  • @nos: Writing the shell script is easy. Using the script as a solution at my workplace adds extra paperwork that I want to avoid. ;) – Christian Jul 09 '12 at 20:05
1

Then this should do it:

$cat file1 >> file2; echo "orange" >> file2;
bluish
  • 26,356
  • 27
  • 122
  • 180
Aftnix
  • 4,461
  • 6
  • 26
  • 43
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
William Pursell
  • 204,365
  • 48
  • 270
  • 300