erase entries in a file

erase entries in a file

Post by Chri » Fri, 15 Jun 2001 23:17:16



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.

 
 
 

erase entries in a file

Post by Mathew Kirsc » Sat, 16 Jun 2001 00:38:24



> 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 ??

You can't really read and write to the same file in a shell script.

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.

 
 
 

erase entries in a file

Post by Matthew Land » Sat, 16 Jun 2001 00:22:08



> 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 can do this fairly easily:

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

Quote:> filename.cut$$; mv filename.cut$$ filename

Perl could also do this even easier, but is less common on all unix
servers.

 - Matt

--
_______________________________________________________________________

   << Comments, views, and opinions are mine alone, not IBM's. >>

 
 
 

erase entries in a file

Post by PT » Sat, 16 Jun 2001 02:59:30



Quote:> 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.

You can use grep also to the same

eg: in a shell script

export var=$$
grep -v host2 filename > /tmp/filename.$var
mv /tmp/filename.$var filename

 
 
 

erase entries in a file

Post by Cyrille Lefevr » Sat, 16 Jun 2001 12:32:54



> 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.

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.

 
 
 

erase entries in a file

Post by Charles Dem » Sat, 16 Jun 2001 17:40:42





>> 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.

perhaps something like this (untested):

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

4. Muting microphone

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?

12. help, erased files

13. root can't erase a file