Using "sed" to append to end of a file

6,485

Solution 1

The usual replacement for shell > with higher/different privileges is:

echo "replace file content with this line" | sudo tee protectedFile >/dev/null

And if you want to append, use -a:

echo "append this line to file" | sudo tee -a protectedFile >/dev/null

Solution 2

Whithout echo:

string=$'"<VirtualHost *:443> \
        ... \
    </VirtualHost>"'

sudo sed -i '$a\'"${string}"'' file

The -i option tells sed to process a files in-place (optionally adding a suffix to the original version).

The sed script:

  1. $ match last line
  2. a\ append text after matching line

It won't work on an empty file though (since there won't be any lines of input to match the last line).

Solution 3

For completeness, if you have ed at hand:

echo "$
a
<VirtualHost *:80>
...
</VirtualHost>
.
w" | sudo ed protectedFile
  1. $ go to last line
  2. a append, followed by data you want to append terminated by a line containing a single dot
  3. w stands for write (file)

The first two commands can be concatenated (yielding $a), yet this won't work on empty file – separately it will, since it means "go to last line" (which is a no-op), followed by "append" rather than "append to last line", which doesn't really exist.

Share:
6,485

Related videos on Youtube

Admin
Author by

Admin

Updated on September 18, 2022

Comments

  • Admin
    Admin over 1 year

    I am currently using sed to write to an apache configuration file from stdin. I am using sed in this script to get around the bash script limition where the calling user does not have privileges to write to the file, so I can't simply echo "..." >> outputfile.conf

    Here is what I have for writing to the file:

    echo "<VirtualHost *:80>
        ...
    </VirtualHost>" | sudo sed -n "w/etc/apache2/sites-available/000-default.conf"
    

    How can I later append to this same file?

    if $enable_ssl; then
        echo "<VirtualHost *:443>
            ...
        </VirtualHost>" | sudo sed <opions-to-sed-here>
    fi
    
    • Arkadiusz Drabczyk
      Arkadiusz Drabczyk over 4 years
      Could you simply use tee -a?
    • Hastur
      Hastur over 4 years
      Don't you have /bin/echo in the system? It is a POSIX specification. In that case you should simply may be able to use something like sudo /bin/echo "whatever" >> outputfile.conf.
    • Admin
      Admin over 4 years
      @Hastur No you can't do that if you are writing to a file owned by root. If am "user" and I run sudo echo "whatever" >> outputfile.conf the writing/appending to output.conf is done by the shell, not by echo. This means I don't have sudo permissions when the output is attempted to be written to the file. Isaac's answer using sudo tee -a is the correct answer. In that scenario tee does have sudo permissions and can write to outputfile.conf.
    • Hastur
      Hastur over 4 years
      @Makerik thanks, I missed that point. BTW, in order to avoid the use of tee (or whatever other executable), it is needed to write something like sudo /bin/bash -c 'echo "whatever" >> outputfile.conf' (or /bin/ash, /bin/dash, etc etc...).
  • Grump
    Grump over 4 years
    I'm not sure why you have the double single quote at the end of the sed command?