|
Home > Archive > Unix Shell > November 2006 > Sed and replacing text
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 |
Sed and replacing text
|
|
| manish 2006-11-08, 1:25 am |
| Hi All
I am trying to change the text "root" from a file to "#root". When i am
running the following command on the prompt but without redirection it
is displaying the correct output on the terminal. But when i redirect
the output to the same file the file loses all its contents.
Pls help.
sed '1,$s/root/#root/' /etc/ftpd/ftpusers > /etc/ftpd/ftpusers
Thanks
Manish.
| |
| RolandRB 2006-11-13, 9:00 am |
|
manish wrote:
> Hi All
>
> I am trying to change the text "root" from a file to "#root". When i am
> running the following command on the prompt but without redirection it
> is displaying the correct output on the terminal. But when i redirect
> the output to the same file the file loses all its contents.
>
> Pls help.
>
> sed '1,$s/root/#root/' /etc/ftpd/ftpusers > /etc/ftpd/ftpusers
>
> Thanks
> Manish.
Then direct the output to a new file with a different name and
afterwards "mv" it over the old file.
| |
| dabs78 2006-11-13, 9:00 am |
| Hi,
You cannot redirect it to the same file.
You will need to redirect to a temp file and then rename that temp
file.
Else open the file in vi and do a search-replace
Hope this helps.
Thanks & Regards,
Amit S. Dabri
manish wrote:
> Hi All
>
> I am trying to change the text "root" from a file to "#root". When i am
> running the following command on the prompt but without redirection it
> is displaying the correct output on the terminal. But when i redirect
> the output to the same file the file loses all its contents.
>
> Pls help.
>
> sed '1,$s/root/#root/' /etc/ftpd/ftpusers > /etc/ftpd/ftpusers
>
> Thanks
> Manish.
| |
| Michael Tosch 2006-11-13, 9:00 am |
| RolandRB wrote:
> manish wrote:
>
> Then direct the output to a new file with a different name and
> afterwards "mv" it over the old file.
>
For safety reasons (e.g. retaining the permissions),
I recommend to not mv, instead do cp then rm.
e.g.
sed '/root/#root/' /etc/ftp/ptpusers > /etc/ftpd/ftpusers.tmp &&
cp /etc/ftpd/ftpusers.tmp /etc/ftpd/ftpusers &&
rm /etc/ftpd/ftpusers.tmp
Alternatively, if you want to keep a .old file:
cp -p /etc/ftpd/ftpusers /etc/ftpd/ftpusers.old &&
sed '/root/#root/' /etc/ftp/ptpusers.old > /etc/ftpd/ftpusers
The && at the end of the line ensures that the next lines are
only run when the exit status is zero.
--
Michael Tosch @ hp : com
| |
| manish 2006-11-13, 9:00 am |
| it works now...
thanks to you all for ur help.
Bye
| |
| Michael Paoli 2006-11-13, 9:00 am |
| manish wrote:
> is displaying the correct output on the terminal. But when i redirect
> the output to the same file the file loses all its contents.
> sed '1,$s/root/#root/' /etc/ftpd/ftpusers > /etc/ftpd/ftpusers
This is a fairly common error, similar to attempting to do:
sort file > file
The problem with these is the shell first does the redirection,
so the output file is opened and trucated. The command then executes,
producing no output, thus one ends up with an empty file. Using >>
instead of > also won't solve the problem, as in that case - depending
on factors such as buffering - one may end up with a very large file,
as one has effectively created an infinite loop.
|
|
|
|
|