Ask a Question related to Linux / Unix Administration, Design and Development.
-
Manoj Panicker #1
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
-
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? -
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... -
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... -
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: -
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... -
Barry Margolin #2
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:Use an ed script:> 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.
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
-
Chris F.A. Johnson #3
Re: How do I edit a file on the command line
On 2004-05-13, Manoj Panicker wrote:
Though it can be done without, a temporary file is the recommended> 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.
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
-
Doug Freyburger #4
Re: How do I edit a file on the command line
Manoj Panicker wrote:
Read up on how the shell processes its commands. I/O redirection>
> # grep -v g x > x
> # cat x
> #
> Can somebody tell me why this is happening?
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
-
Ian Wilson #5
Re: How do I edit a file on the command line
Manoj Panicker wrote:
<snip>> 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.
This must be a FAQ. The process starts writing to the output file> Can somebody tell me why this is happening?
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



Reply With Quote

