INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

grep issue in ksh script

grep issue in ksh script

(OP)
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

RE: grep issue in ksh script

(OP)
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 ???

RE: grep issue in ksh script

(OP)
Ya,,, I've tried grep, egrep, fgrep,, Everything returns a RC:1

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

RE: grep issue in ksh script



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

RE: grep issue in ksh script



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

RE: grep issue in ksh script

(OP)
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.

RE: grep issue in ksh script


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

RE: grep issue in ksh script

@tmd1332 .. recently one very simple one stumped me wasting hours.
Probably not related to your problem, but you never know lol .. jump down to '20 Nov 13 15:47' on this one that tripped me up (once again) http://www.tek-tips.com/viewthread.cfm?qid=1721210

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close