Results 1 to 4 of 4

Thread: A CLI file editing question.

  1. #1
    Join Date
    Jul 2010
    Location
    Florida
    Posts
    22

    Default A CLI file editing question.

    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:

    PATH=/bin:/sbin


    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:

    PATH=/bin:/sbin
    PATH=:/usr/bin:/usr/sbin

    What I want is for it to look like:


    PATH=/bin:/sbin:/usr/bin:/usr/sbin


    Is there a way to get this result via command line?

  2. #2

    Default Re: A CLI file editing question.

    cp env.logon{,.orig}
    sed 's|PATH=.*|&:/usr/bin:/usr/sbin|' env.logon.orig > env.logon

  3. #3
    Join Date
    Jul 2010
    Location
    Florida
    Posts
    22

    Default Re: A CLI file editing question.

    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.

  4. #4

    Default Re: A CLI file editing question.

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •