Solved

UNIX script Error

Posted on 2012-04-12
5
309 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Problem Imaging Computers With Clonezilla 2 95
unix scripting question 1 104
How to Change Interface on Cisco IOU 11 111
Invalid password trying to cannect to Unix server 2 88
I have been running these systems for a few years now and I am just very happy with them.   I just wanted to share the manual that I have created for upgrades and other things.  Oooh yes! FreeBSD makes me happy (as a server), no maintenance and I al…
Every server (virtual or physical) needs a console: and the console can be provided through hardware directly connected, software for remote connections, local connections, through a KVM, etc. This document explains the different types of consol…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:

821 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