Unix administration - How do I edit a file on the command line

This is Interesting: Free IT Magazines  
Home > Archive > Unix administration > May 2004 > How do I edit a file on the command line





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author How do I edit a file on the command line
Manoj Panicker

2004-05-12, 8:46 pm

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
Barry Margolin

2004-05-12, 8:46 pm

In article <a449c62c.0405121615.275b8727@posting.google.com>,
manojmpanicker@yahoo.com (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, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
Chris F.A. Johnson

2004-05-13, 6:36 am

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 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
Doug Freyburger

2004-05-13, 12:52 pm

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.
Ian Wilson

2004-05-14, 6:34 pm

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.

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com