How do you append text to an entry in an existing file? For example, lets say I
have a file called “env.logon” in /home/myself/bin that contains the following text:
If I wanted to add, via command line, “:/usr/bin:/usr/sbin” to the PATH= and I
used the “echo” command (echo "PATH=:/usr/bin:/usr/sbin) it would create a second entry and my file would look like:
What I want is for it to look like:
Is there a way to get this result via command line?
sed ‘s|PATH=.*|&:/usr/bin:/usr/sbin|’ env.logon.orig > env.logon
Thank you very much.
Would you mind either explaining the syntax to me or pointing somewhere that does? Is this just standard bash scripting techniques? If it is I have a book I can look it up in.
The first command uses bash braces expansion and is equivalent to: cp env.logon env.logon.orig .
The second command uses ‘sed’, a ‘stream editor’. It’s a basic Unix tool. You’ll find plenty of documentation and manuals online. Once you learn to work with ‘regular expressions’, it’s a very powerfull tool. But carefull while using sed : ALWAYS make a copy of your original file. By the smallest syntax error, sed produces an empty output resulting in a file with 0 size when redirected.