Solved

UNIX script Error

Posted on 2012-04-12
5
310 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java core in Solaris 10 1 325
Unix Command -- Challenging  question 7 105
to update from RHEL 6.0 to 6.8 anything needed other than "yum update"? 7 137
Access_log 17 181
This tech tip describes how to install the Solaris Operating System from a tape backup that was created using the Solaris flash archive utility. I have used this procedure on the Solaris 8 and 9 OS, and it shoudl also work well on the Solaris 10 rel…
Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
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…
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.
Suggested Courses

742 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