Link to home
Start Free TrialLog in
Avatar of uco
uco

asked on

Searching for strings in one file from another file

I have a file A  with four Id's . I need to check if these Id's  exist in file B and send a simple email if a particular id does not exist in B
For example file A has
Id's
1001
1002
1003
1004

If Id 1003 does not exist in file B a email should be sent out that 1003 does not exist

the lenght of Id is fixed i.e 4, and Id in file B is the first column  and file B could be having 1000's of rows
Attached are sample files
ids.txt
trailer.txt
Avatar of woolmilkporc
woolmilkporc
Flag of Germany image

while read ID; do
 if ! grep -q ^$ID fileB; then
   echo $ID not found in fileB
 fi ; done < fileA | mail -s "Missing ID(s)" recipient@domain.tld
Avatar of uco
uco

ASKER

we are giving fileA  as input at the end ?
Yep, right after "done", as "input" for the search patterns.

The file to search in is the one just after "...grep -q ^$ID"
ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of uco

ASKER

while read ID; do
 if ! grep -q ^$ID fileB; then
   echo $ID not found in fileB
 fi ; done < fileA | mail -s "Missing ID(s)" recipient@domain.tld

It is only showing 1001 is missing in fileB, eventhough 1003 is also missing.
Also it has to look only the first column in fileB and column lenght is 4.
email has to be sent only once as summary of id's missing

thanks
I see a 1003 in the last line of trailer.txt
1) 1003 is not missing, at least not in the sample you posted. Look at the last line.

2) It does look only at the first column, see the caret  ("^$ID"). Length 4 is given by the length of the pattern in fileA, which you said is 4.

3) Email is sent only once. See the pipe to mail at the very end (after ... done).

Why do people post flack before testing?
Avatar of uco

ASKER

I did test it , Please use this file , for this one it is showing 1001 is missing , 1004 is also missing
Thanks for the  help
trailer.txt
This is what my script gives with your new file:

1001 not found in trailer.txt
1004 not found in trailer.txt

Where's the problem?
Avatar of uco

ASKER

this is what I am doing
I am appending mail content  to empty file

while read ID; do
 if ! grep -q ^$ID trailer.txt; then
   echo  "  $ID not found in trailer        :    " >> test_file.txt  
 fi ; done < ids.txt
 mailx -s "ISSUE: " -c $Mail    < test_file.txt

Anything wrong here?
Nothing wrong, but why this intermediate file?

My original script accomplishes the same result without creating such a file inbetween.

Your mailx command lacks the recipient's address. "-c" specifies the recipient(s) of a copy.
Avatar of uco

ASKER

Ok I removed the extra file , I am running exactly the same script as you gave  but still I am getting the

ID not found in trailer :
1001 not found in trailer :

2nd
I executed with trailer file completely deleted, but then it is only showing until 1003
ID not found ID :
1001 not found ID :
1002 not found ID :
1003 not found ID :

I think it is not going to the end of the fileA (ID file)
1)  Does this line "ID not found in trailer :" bother you?
That's because you have the header line "ID" in ids.txt but not in the trailer file. We could easily filter it out if needed:

while read ID; do
 if ! grep -q ^$ID trailer.txt; then
    [[ "$ID" != "ID" ]] && echo $ID not found in trailer.txt
 fi ; done < ids.txt | mail -s "Missing ID(s)" recipient@domain.tld

2) I think I can't understand your second issue.

With an empty "trailer.txt" file I get this (old version):

ID not found in trailer.txt
1001 not found in trailer.txt
1002 not found in trailer.txt
1003 not found in trailer.txt
1004 not found in trailer.txt

and with trailer.txt completely deleted (= removed) I additionally get error messages from "grep", which go to the terminal but not into the mail:

grep: trailer.txt: No such file or directory
ID not found in trailer.txt
grep: trailer.txt: No such file or directory
1001 not found in trailer.txt
grep: trailer.txt: No such file or directory
1002 not found in trailer.txt
grep: trailer.txt: No such file or directory
1003 not found in trailer.txt
grep: trailer.txt: No such file or directory
1004 not found in trailer.txt

I admit that the second output isn't really "nice" but it isn't wrong either, and we can easily suppress those error messages, if desired.

In any case ids.txt is read up to the end. Are you sure that you're using the version of ids.txt which you posted in your question?
Which shell are you using, and which version?
Some versions of some shells have had issues with redirection into a do loop
Avatar of uco

ASKER

I am using bash shell

I have no issue with "ID not found in trailer :"

I am using the same version of id file that I sent . But number of my id's could increase or decrease

This one gives correct results , but I need to print  them like
"ID 1001 is missing"  


awk '!/ID/{sub(/\r/,"");print}' ids.txt | sort > ids.sort
awk '{print $1}' trailer.txt | sort > trailer.sort
comm -23 ids.sort trailer.sort
ids.txt
trailer.txt
comm -23 ids.sort trailer.sort | awk '{print "ID "$1" is missing" }'