Grep and delete lines except the lines with strings

Hi

I am writing a script which should read a file and search for certain strings 'approved' or 'removed' and retain only those lines that contain the above strings.

Ex: file name 'test'
test:

approved package
waiting for approval package
disapproved package
removed package
approved package
approved package

Here i need to delete all the lines except those that contain either 'approved' or 'removed'.

How can i do that.

$ cat test
approved package
waiting for approval package
disapproved package
removed package
approved package
approved package

$ egrep -E 'approved|removed' test
approved package
disapproved package
removed package
approved package
approved package

Thank you ... but i have two problems

1) egrep -E does not work. it returns 'Invalid options'

2) as per the above command 'disapproved' is not removed from file
i want that line also to be removed with 'removed' or 'approved' string option.

using sed:

 sed -i -nl -e '/\<approved\>\|\<removed\>/ {p;} ' test

what you need is word boundaries:

 egrep -E '\<approved\>|\<removed\>' test

YMMV:

egrep '^(approved|removed)' test

sir,

i get the reply as

sed: illegal option -- i

thank you vgrep99...

but still i get the 'disapproved' line in the file. I want to remove all the lines that does not have either 'approved' or 'removed' as a seperate string.

egrep '^(approved|removed)' test

this works well and gives the desired output. but the file is not changed. is there anyway that the file is modified with out redirecting??

your sed command does not support the -i (inline edit) option. try using the egrep command instead.

poor-man 'sed -i':

(rm test ; sed -n '/^approved/p;/^removed/p' > test ;) < test

i don't whant to create a new post, so i'll ask here. How to grep the lines containing sertain two or more words lets say i have:

Thu Dec  4 08:00:14 2008 : Auth: Login OK: [boda7805] (from client LINKSYS3 port 33 cli 0015afecda17)
Thu Dec  4 08:00:50 2008 : Error: rlm_eap: UserIdentity Unknown 
Thu Dec  4 08:00:50 2008 : Error: rlm_eap: Identity Unknown, authentication failed
Thu Dec  4 08:00:50 2008 : Auth: Login incorrect: [<no User-Name attribute>] (from client WILI-08 port 8 cli 00-17-31-AA-2D-77)
Thu Dec  4 08:00:52 2008 : Auth: Login incorrect: [<no User-Name attribute>] (from client WILI-08 port 8 cli 00-17-31-AA-2D-77)
Thu Dec  4 08:03:06 2008 : Auth: Login incorrect (rlm_ldap: User not found): [ADMIN\\Adminas] (from client WILI-08 port 0 via TLS tunnel)
Thu Dec  4 08:22:15 2008 : Auth: Login OK: [dmal5609] (from client WILI-08 port 0 via TLS tunnel)
Thu Dec  4 08:22:15 2008 : Auth: Login OK: [dmal5609] (from client WILI-08 port 8 cli 00-15-AF-CB-68-67)

the output should look like this:
Thu Dec 4 08:00:14 2008 : Auth: Login OK: [boda7805] (from client LINKSYS3 port 33 cli 0015afecda17)
Thu Dec 4 08:22:15 2008 : Auth: Login OK: [dmal5609] (from client WILI-08 port 8 cli 00-15-AF-CB-68-67)

and i am greping for strings - (Auth: Login OK:) and (cli)

Please create a new thread and read the rules - don't hijack other people's threads!

(rm test ; sed -n '/^approved/p;/^removed/p' > test ;) < test

Can you explain me the above command?

Hi,

Try this script

#cat infile
approved package
waiting for approval package
disapproved package
removed package
approved package
approved package

#!/bin/sh

while read line
do
echo $line > tempfile
CHK=`grep -i "approved" tempfile|wc -l |awk '{print $1}'`
CHK1=`grep -i "removed" tempfile|wc -l |awk '{print $1}'`
if [ $CHK -gt 0 -o $CHK1 -gt 0 ]
then
        echo $line
fi
rm tempfile
done < infile