Solved

UNIX script Error

Posted on 2012-04-12
5
306 Views
Last Modified: 2012-06-27
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
Comment
Question by:mnis2008
  • 3
5 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 37840644
if  egrep -q $i compare.txt ; then
        echo "$i doesnot exist"
        fi
0
 
LVL 30

Expert Comment

by:Kerem ERSOY
ID: 37840658
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
 
LVL 30

Expert Comment

by:Kerem ERSOY
ID: 37840663
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
 
LVL 30

Accepted Solution

by:
Kerem ERSOY earned 500 total points
ID: 37840669
even shorter:

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

Expert Comment

by:Tintin
ID: 37847916
or even shorter
grep -vf compare.txt test.txt | xargs -i echo {} not exist

Open in new window

0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Introduction Regular patching is part of a system administrator's tasks. However, many patches require that the system be in single-user mode before they can be installed. A cluster patch in particular can take quite a while to apply if the machine…
FreeBSD on EC2 FreeBSD (https://www.freebsd.org) is a robust Unix-like operating system that has been around for many years. FreeBSD is available on Amazon EC2 through Amazon Machine Images (AMIs) provided by FreeBSD developer and security office…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now