• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 319
  • Last Modified:

UNIX script Error

I have a file : cat test.txt
22CF
22CE
22D1
846
68A
961
4EA
959
437
951

I have another file compare.txt, where I am trying to compare each number above and if it doesnot exist I need to get an output - Doesnot exist

My script is giving me an error

for i in `cat test.txt`
do  
        if [ "$(egrep $i compare.txt)" -eq 0 ]; then
        echo "$i doesnot exist"
        fi
done

Can someone please help
0
mnis2008
Asked:
mnis2008
  • 3
1 Solution
 
ozoCommented:
if  egrep -q $i compare.txt ; then
        echo "$i doesnot exist"
        fi
0
 
Kerem ERSOYPresidentCommented:
Hi,

for i in `cat test.txt`
do
  grep $i compare.txt > /dev/null
  if [ $? -ne 0 ]
  then
    echo $i "not exist"
  fi
done

Open in new window


Should do.

Cheers,
K.
0
 
Kerem ERSOYPresidentCommented:
A shorter version should be:

for i in `cat test.txt`
do
  ! egrep -q $i compare.txt && echo $i "not exist"
done

Open in new window

0
 
Kerem ERSOYPresidentCommented:
even shorter:

for i in `cat test.txt`; do  ! egrep -q $i compare.txt && echo $i "not exist"; done
0
 
TintinCommented:
or even shorter
grep -vf compare.txt test.txt | xargs -i echo {} not exist

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now