Hi,
I want to do a script which erase some entries in a file. For example I've
got these entries :
Host1
Host2
Host3
And with a sed command erase the enty Host1 and save it in the same file.
Can anyone helps me ??
Thanks,
Chris.
I want to do a script which erase some entries in a file. For example I've
got these entries :
Host1
Host2
Host3
And with a sed command erase the enty Host1 and save it in the same file.
Can anyone helps me ??
Thanks,
Chris.
> Host1
> Host2
> Host3
> And with a sed command erase the enty Host1 and save it in the same file.
> Can anyone helps me ??
What you can do is make a temporary copy of the original file, read from it,
then write your output back to the original.
#!/bin/sh
#WARNING! THIS IS UNTESTED CODE. NO GUARANTEES, EXPRESSED OR IMPLIED ARE MADE.
cp originalfile temporaryfile
sed '/Host1/D' temporaryfile > originalfile
rm temporaryfile
You'll want to add logic to the script to make sure things went as planned.
> Hi,
> I want to do a script which erase some entries in a file. For example I've
> got these entries :
> Host1
> Host2
> Host3
> And with a sed command erase the enty Host1 and save it in the same file.
> Can anyone helps me ??
> Thanks,
> Chris.
awk '{if (/Host1/)
{print $0 >"/match/filename" }
else
{print $0}} ' filename
The command above puts the cut portion into the file /match/filename.
The "rest" of the file will be put to stdout. Make sure this part
works. If it does and you want filename to have only the rest of the
lines... Add the following to the last line
Perl could also do this even easier, but is less common on all unixQuote:> filename.cut$$; mv filename.cut$$ filename
- Matt
--
_______________________________________________________________________
<< Comments, views, and opinions are mine alone, not IBM's. >>
You can use grep also to the sameQuote:> Hi,
> I want to do a script which erase some entries in a file. For example I've
> got these entries :
> Host1
> Host2
> Host3
> And with a sed command erase the enty Host1 and save it in the same file.
> Can anyone helps me ??
> Thanks,
> Chris.
eg: in a shell script
export var=$$
grep -v host2 filename > /tmp/filename.$var
mv /tmp/filename.$var filename
> I want to do a script which erase some entries in a file. For example I've
> got these entries :
> Host1
> Host2
> Host3
> And with a sed command erase the enty Host1 and save it in the same file.
or
ed - file << EOF
g/Host1/d
w
q
EOF
I'm almost sure this could also be done using perl :
perl -pi -e 'next if /Host1/' file
^^^^ but this dosn't work.
Cyrille.
--
Supprimer "%no-spam" et ".invalid" pour me repondre. | about who it chooses
Remove "%no-spam" and ".invalid" to answer me back. | to be friends with.
>> I want to do a script which erase some entries in a file. For example I've
>> got these entries :
>> Host1
>> Host2
>> Host3
>> And with a sed command erase the enty Host1 and save it in the same file.
>(echo "g/Host1/d"; echo w; echo q) | ed - file
>or
>ed - file << EOF
>g/Host1/d
>w
>q
>EOF
>I'm almost sure this could also be done using perl :
>perl -pi -e 'next if /Host1/' file
> ^^^^ but this dosn't work.
perl -pi -e 'print if !/Host1/' file
or
perl -pi -e 'if !/Host1/ print' file
Chuck Demas
--
Eat Healthy | _ _ | Nothing would be done at all,
Die Anyway | v | That no one could find fault with it.
1. Deleting entries froma file from a list of entries
I have a file containing a list of usernames, and I want to edit the
shadow files and passwd files so that these usernames(and all other
information pertaining to the particular user) are deleted from the file.
I know that there is a way in sed to do this, but I am not very well
versed in sed.
Paul White
2. Can AIX authenticate using LDAP server without NIS?
3. Does tctl erase really erase the tape
5. Change "stty erase ^?" for "stty erase ^H"
6. An Open letter to the OSS Community
7. too many entries too many entries too mant entries in the KDE menu
8. Linux - gcc and curses functions
9. Netscape plug-ins, helper files, Acrobat etc.
10. Compare file modification date using tcsh built-ins
11. Is it possible to protect files from erasing from network?