Solved

UNIX script Error

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
netstat -a in AIX unix 8 43
OpenLDAP Proxy to Active Directy 6 287
check unix curl command return value 7 117
Showing a username in UNIX and using that name in the script 4 35
When you do backups in the Solaris Operating System, the file system must be inactive. Otherwise, the output may be inconsistent. A file system is inactive when it's unmounted or it's write-locked by the operating system. Although the fssnap utility…
I promised to write further about my project, and here I am.  First, I needed to setup the Primary Server.  You can read how in this article: Setup FreeBSD Server with full HDD encryption (http://www.experts-exchange.com/OS/Unix/BSD/FreeBSD/A_3660-S…
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.:
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

778 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