PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Forums Hébergement > Forum Serveur - Sécurité et techniques > comp.unix.shell > Sed and replacing text
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
comp.unix.shell Using and programming the Unix shell.

Sed and replacing text

Réponse
 
LinkBack Outils de la discussion
Vieux 08/11/2006, 07h08   #1
manish
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Sed and replacing text

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.

  Réponse avec citation
Vieux 08/11/2006, 07h27   #2
RolandRB
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Sed and replacing text


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.

  Réponse avec citation
Vieux 08/11/2006, 12h02   #3
dabs78
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Sed and replacing text

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.


  Réponse avec citation
Vieux 08/11/2006, 12h33   #4
Michael Tosch
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Sed and replacing text

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
  Réponse avec citation
Vieux 09/11/2006, 05h07   #5
manish
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Sed and replacing text

it works now...

thanks to you all for ur .

Bye

  Réponse avec citation
Vieux 10/11/2006, 13h19   #6
Michael Paoli
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Sed and replacing text

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.

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 05h17.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 1,40701 seconds with 14 queries