How do I edit a file on the command line

Ask a Question related to Linux / Unix Administration, Design and Development.

  1. #1

    Default How do I edit a file on the command line

    Hello gurus,

    I need to delete a line from a file and I need to do it on the command
    line. I can delete the line using sed or grep -v on the file but I
    start having problems when I write back the file. For illustrations
    sake, I take a file called 'x', which has the following lines:

    # cat x
    a
    d
    c
    d
    e
    f
    g
    h

    I need to delete the line that matches the pattern 'g', so I do "grep
    -v g x "and I get:
    # grep -v g x
    a
    d
    c
    d
    e
    f
    h

    If I try to redirect the output to the same file, I see that the file
    becomes empty.

    # grep -v g x > x
    # cat x
    #


    But if append to the file, I see the old contents and the edited one.

    # grep -v g x >> x
    # cat x
    a
    d
    c
    d
    e
    f
    g
    h

    a
    d
    c
    d
    e
    f
    h


    Can somebody tell me why this is happening? I need the line deleted
    and the contents written back to the same file. Now, I can do it by
    using a temporary file, but I want to learn if it can be done using a
    single command line.

    Thanks
    Manoj
    Manoj Panicker Guest

  2. Similar Questions and Discussions

    1. command line
      In windows thru the GUI, Control Panel -> Network Connection you'll see all your network interfaces. How one do it from the command line?
    2. RUN/execute a Command-Line command from an ASP page
      Hi, I need to RUN/execute a Command-Line command from an ASP page. This is the command: sse45.exe -i k:\o\2.wmv -o k:\o\2.shh -w 128 -df 0 -m 2...
    3. RUN/execute a Command-Line command from an ASP page.
      Hi, I need to RUN/execute a Command-Line command from an ASP page. This is the command: sse45.exe -i k:\o\2.wmv -o k:\o\2.shh -w 128 -df 0 -m 2...
    4. Piping to windows command line .rb file doesn't work
      If I do this: home> type cat.rb I get this output: print $stdin.readlines If I do this:
    5. copying value of DDL in a Datagrid "pre-edit command" to value in "post edit command"
      Hi all! I have Datagrid. Within that datagrid I have a drop down list that I set up some values in (a "collection") I set up an edit command...
  3. #2

    Default Re: How do I edit a file on the command line

    In article <a449c62c.0405121615.275b8727@posting.google.com >,
    [email]manojmpanicker@yahoo.com[/email] (Manoj Panicker) wrote:
    > Can somebody tell me why this is happening? I need the line deleted
    > and the contents written back to the same file. Now, I can do it by
    > using a temporary file, but I want to learn if it can be done using a
    > single command line.
    Use an ed script:

    ed filename <<EOF
    1,$g/regexp/d
    w
    q
    EOF

    --
    Barry Margolin, [email]barmar@alum.mit.edu[/email]
    Arlington, MA
    *** PLEASE post questions in newsgroups, not directly to me ***
    Barry Margolin Guest

  4. #3

    Default Re: How do I edit a file on the command line

    On 2004-05-13, Manoj Panicker wrote:
    > Hello gurus,
    >
    > I need to delete a line from a file and I need to do it on the command
    > line. I can delete the line using sed or grep -v on the file but I
    > start having problems when I write back the file. For illustrations
    > sake, I take a file called 'x', which has the following lines:
    >
    > # cat x
    > a
    > d
    > c
    > d
    > e
    > f
    > g
    > h
    >
    > I need to delete the line that matches the pattern 'g', so I do "grep
    > -v g x "and I get:
    > # grep -v g x
    > a
    > d
    > c
    > d
    > e
    > f
    > h
    >
    > If I try to redirect the output to the same file, I see that the file
    > becomes empty.
    >
    > # grep -v g x > x
    > # cat x
    > #
    >
    >
    > But if append to the file, I see the old contents and the edited one.
    >
    > # grep -v g x >> x
    > # cat x
    > a
    > d
    > c
    > d
    > e
    > f
    > g
    > h
    >
    > a
    > d
    > c
    > d
    > e
    > f
    > h
    >
    >
    > Can somebody tell me why this is happening? I need the line deleted
    > and the contents written back to the same file. Now, I can do it by
    > using a temporary file, but I want to learn if it can be done using a
    > single command line.
    Though it can be done without, a temporary file is the recommended
    method. It's much safer. (If you have to ask how to do it, you need
    the safety belt.)

    --
    Chris F.A. Johnson [url]http://cfaj.freeshell.org/shell[/url]
    ================================================== =================
    My code (if any) in this post is copyright 2004, Chris F.A. Johnson
    and may be copied under the terms of the GNU General Public License
    Chris F.A. Johnson Guest

  5. #4

    Default Re: How do I edit a file on the command line

    Manoj Panicker wrote:
    >
    > # grep -v g x > x
    > # cat x
    > #
    > Can somebody tell me why this is happening?
    Read up on how the shell processes its commands. I/O redirection
    is done before any exec calls. Both I/O rediction and variable
    expansion even. So if you do I/O rediction to a file, that file
    will be emptied first before the command is called. If the same
    name is in the command line, it will then be an empty file.

    Redirect to a temporary file, then rename in another command.
    Doug Freyburger Guest

  6. #5

    Default Re: How do I edit a file on the command line

    Manoj Panicker wrote:
    > Hello gurus,
    >
    > I need to delete a line from a file and I need to do it on the command
    > line. I can delete the line using sed or grep -v on the file but I
    > start having problems when I write back the file.
    <snip>
    > If I try to redirect the output to the same file, I see that the file
    > becomes empty.
    <snip>
    > Can somebody tell me why this is happening?
    This must be a FAQ. The process starts writing to the output file
    immediately. Since the file is also the input file there's nothing left
    to read. You need a temporary file. Some tools do this implicitly for
    you if you ask them ...

    I like:
    perl -n -i -e 'print unless /g/' filename ...

    or probably more sensibly:
    perl -n -i -e 'print unless /^g$/' filename ...

    or maybe:
    perl -n -i -e 'print unless /^\s*g\s*$/' filename ...

    depending on what your data really looks like.

    But there's lots of ways to do this without using perl.

    Ian Wilson Guest

Posting Permissions

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139