Tech Support > Operating Systems > UNIX / Variants > How do I edit a file on the command line
How do I edit a file on the command line
Posted by Manoj Panicker on May 13th, 2004


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

Posted by Barry Margolin on May 13th, 2004


In article <a449c62c.0405121615.275b8727@posting.google.com>,
manojmpanicker@yahoo.com (Manoj Panicker) wrote:
Use an ed script:

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

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

Posted by Chris F.A. Johnson on May 13th, 2004


On 2004-05-13, Manoj Panicker wrote:
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 http://cfaj.freeshell.org/shell
================================================== =================
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

Posted by Doug Freyburger on May 13th, 2004


Manoj Panicker wrote:
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.

Posted by Ian Wilson on May 14th, 2004


Manoj Panicker wrote:
<snip>

<snip>

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.



Similar Posts