|
|
|
|
||||||
| comp.unix.shell Using and programming the Unix shell. |
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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 . sed '1,$s/root/#root/' /etc/ftpd/ftpusers > /etc/ftpd/ftpusers Thanks Manish. |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
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 . > > 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. |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
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 s. 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 . > > sed '1,$s/root/#root/' /etc/ftpd/ftpusers > /etc/ftpd/ftpusers > > Thanks > Manish. |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
RolandRB wrote:
> 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 . >> >> 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. > 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 |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
it works now...
thanks to you all for ur . Bye |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
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. |
|
![]() |
| Outils de la discussion | |
|
|