Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

grep issue in ksh script

Status
Not open for further replies.

tmd1332

Technical User
Feb 10, 2010
4
Hi Guys. Hoping you can help me. I've been scripting forever, but this one has me stumped.

File1 Contents:
Job: ZDUMM2 StDate: 20131114 StTime: 045241 Server: vq2ua740_ECQ_00 Status: Failed
Job: ZDUMM1 StDate: 20131114 StTime: 045241 Server: vq2ua740_ECQ_00 Status: Failed

File2 Contents:
Job: ZDUMM1 StDate: 20131114 StTime: 045241 Server: vq2ua740_ECQ_00 Status: Failed

Notice the record in File2 exist in File1 also.

From the command line, this Returns a RC:0 as it should

grep "Job: ZDUMM1 StDate: 20131114 StTime: 045241 Server: vq2ua740_ECQ_00 Status: Failed" File1
Job: ZDUMM1 StDate: 20131114 StTime: 045241 Server: vq2ua740_ECQ_00 Status: Failed
$ echo $?
0

But when scripted in a loop, no matter what I do, the grep always returns a RC:1.
I've tried every combination of quotes, single, double, escaping the quotes, enclosing single quotes
inside of double quotes. Not matter what I do I get RC:1

This should be something simple, but it's got me stumped.
I've also tried egrep and fgrep. Same results

Here's my simple (but not working) script:

#!/usr/bin/ksh
#set -x

cat File2 | while read record;do
grep "$record" File1
if [ $? -eq 0 ];then
echo "Found it."
fi
done

 
With this script:
Code:
while read record; do
  grep "$record" File1 && echo "Found it."
done < File2
I get this output with the FileX you posted:
Job: ZDUMM1 StDate: 20131114 StTime: 045241 Server: vq2ua740_ECQ_00 Status: Failed
Found it.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
PHV thanks so much for the tip. However, I tried your exact code and still returns no output
We're an AIX Unix system. This is what I get

$ cat test1
#!/usr/bin/ksh
while read record; do
grep "$record" File1 && echo "Found it."
done < File2


$ test1
$

Returns nothing ???
 
What about using fgrep ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Ya,,, I've tried grep, egrep, fgrep,, Everything returns a RC:1

It defies logic. It should be so simple. Something funky here.
 


How about ...

Code:
join File2 File1 | sort -u
?
[3eyes]

----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 


Or ....

Code:
[ `join File2 File1|sort -u|wc -l` -gt 0 ] && echo "Found it!!"
[noevil]



----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 
LKBrwnDBA (MIS) Thanks for the tip,, However, there's going to be multiple entries in both Files.

I need the single recored(s) one by one as they're found in the file so I can alert on them.
 

Use commands from my first post or supply a better data sample.
[hairpull]

----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top