Operating System - HP-UX
1834254 Members
2609 Online
110066 Solutions
New Discussion

Re: help shell script: for and while

 
Jairo Campana
Trusted Contributor

help shell script: for and while

hello i have a problem
in fileA I need to exclude domains from email that belong to another file
example fileA
cat fileA
yahoo.com
hotmail.com
hp.com
********************************
cat fileB
jhon@yahoo.com
postmaster@hotmail.com
**********************************
grep -v hotmail.com fileB --->this works

in script:
cat fileA|while read domain; do grep -v $domain fileB ; done

this not works

for i in `cat fileA`
do
grep -v $i fileB
done

this not works

the problem is the name@domain grep -v domain
legionx
6 REPLIES 6
A. Clay Stephenson
Acclaimed Contributor

Re: help shell script: for and while

I'll give you a hint and you take it from there:

cat fileA | while read domain
do
grep -q "${domain}" fileB 2>/dev/null
STAT=${?}
if [[ ${STAT} -eq 1 ]]
then
echo "${domain} not found in fileB"
fi
done

You only care if the pattern was found or not so use qrep -q and examine the exit status: 0 - was found 1 - was not found; other values would indicate an error
If it ain't broke, I can fix that.
Jairo Campana
Trusted Contributor

Re: help shell script: for and while

I do not obtain anything
$ cat fileA|while read domain; do grep -q "${domain}" fileB; stat=$?; if [[ ${STAT} -eq 1 ]]; then echo "${domain} not found in fileB"; fi; done
$ echo $?
0
$

the $? is 0


fileA
hotmail.com
yahoo.com
hp.com

the fileB
john@yahoo.com
postmaster@hotmail.com
carl@next.com

grep -v hotmail.com fileB
grep -v yahoo.com fileB
...
I want to obtain fileC using while true

cat fileC
carl@next.com
legionx
A. Clay Stephenson
Acclaimed Contributor

Re: help shell script: for and while

Ok, I understand better. We can use grep -E -v and supply multiple -e patterns and you should get all the non-excluded lines.

Try this:
------------------------------------
#!/usr/bin/sh

typeset C=""
typeset X=""
cat fileA | while read X
do
C="${C} -e \"${X}\""
done
eval grep -E -v "${C}" fileB
If it ain't broke, I can fix that.
Jairo Campana
Trusted Contributor

Re: help shell script: for and while

cat fileB|while read X; do C="${C} -e \"${X}\""; eval grep -E -v "${C}"fileA ;done
postmaster@hotmail.com
carl@next.com

ok , but it display the account postmaster@hotmail

I change the line script fileB for fileA position.


exclude account hotmail and yahoo.com of the fileB
that it contains the file fileA : yahoo.com hotmail.com



legionx
James R. Ferguson
Acclaimed Contributor

Re: help shell script: for and while

Hi Jairo:

I posted this in your other thread:

http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=1117174

# cat fileA
yahoo.com
hotmail.com
hp.com

# cat fileB
jhon@yahoo.com
postmaster@hotmail.com
somebody@xyz.com
anotherbody@bigdot.com

# grep -F -v -f fileA fileB
somebody@xyz.com
anotherbody@bigdot.com

Regards!

...JRF...
Jairo Campana
Trusted Contributor

Re: help shell script: for and while

Thanks, James submit points in the other post.
legionx