Visit Our UNIX and Linux User Community


If test grep.... always returns 0 status


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting If test grep.... always returns 0 status
# 1  
Old 06-22-2011
If test grep.... always returns 0 status

Hi all. I am trying to compare and filter two files. I have a bigfile.txt of names and ids and a smallfile.txt of ids only. What I am trying to do is use a while read loop to read the ids in the bigfile and then echo the name and id only if the id exists in the small file. Basically, I'm trying to find the names that correspond to the ids in the small file.

Here's what I've got so far:
Code:
big=bigfile.txt
small=smallfile.txt

while read user id
do
    if test 'grep -q $id $small'; then
        echo "ID exists"
    else
        echo "ID does not exist"
    fi
done < $big

My problem now is that the first condition is always evaluated as true. No matter whether the id exists or not in the smallfile, the output is always the same - "ID Exists" for every line of input in bigfile.

I've manually confirmed that the exit code should be non-0 for multiple ids in bigfile. Any ideas on what I'm doing wrong?
# 2  
Old 06-22-2011
Code:
nawk 'FNR==NR {small[$1];next} {print $1, ($1 in small)?"exists":"does not exist"}' small big

# 3  
Old 06-22-2011
Actually what you are trying to do ?

what is your exact need.?
# 4  
Old 06-22-2011
try the below
Code:
big=bigfile.txt
small=smallfile.txt

while read user id
do
     grep -q $id $small ; 
     if [ $? -eq 0 ]
     then
        echo "ID exists"
     else
        echo "ID does not exist"
    fi
done < $big

# 5  
Old 06-22-2011
That would be because your grep command isn't actually executed, but is a plain string, which is always true. Lose the test command and the quotes, as test is only needed when you're doing a comparison, but not when the command returns a true or false exit code.
# 6  
Old 06-22-2011
From looking at your grep attempt, you have assumed that the id cannot occur as part of a name. The following makes the same assumption, but it's more efficient than looping and repeatedly reading a file. The output should be lines in bigfile.txt whose id is present in smallfile.txt:
Code:
grep -Ff smallfile.txt bigfile.txt

Regards,
Alister

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Test with non-zero status does not exit shell

Hi there, I'm very used to use set -e to break my scripts if any command exits with a non-zero status. As a policy, I'm willingly expecting echo hello | grep a to break the script. The commands test 1 -eq $1 && echo hello exits with a non-zero status if $1 is not 1. BUT... It doesn't break... (2 Replies)
Discussion started by: chebarbudo
2 Replies

2. Shell Programming and Scripting

Test exit status of last cmd via ssh

see below for a housekeeping script which constructs an ssh cmd using some server/path/sudo info found in $HRINST. the script should hop to each server and if it finds a file to cleanup, moves it to the archive dir if there is nothing to move, it should report so and email the output ... (3 Replies)
Discussion started by: jack.bauer
3 Replies

3. Shell Programming and Scripting

How to test for the ssh exit status in script?

Hello; I regularly run monitoring scripts over ssh to monitoring scripts But whenever a server is hung or in maintenance mode, my script hangs.. Are there anyways to trap exit status and be on my way ?? Looked at the ssh manpage and all I can see is a "-q" option for quiet mode .. Thank... (2 Replies)
Discussion started by: delphys
2 Replies

4. UNIX for Dummies Questions & Answers

grep returns many values into a variable error

I get an error when I grep my results into a variable when the grep finds more than one file. FND="$(find ediout* -mmin -60)" if ; then STR="$(find ediout* -mmin -60 -exec grep -l "ERRORS Encountered" {} +)" fi When there is only one ediout file it works fine. How do I... (11 Replies)
Discussion started by: aispg8
11 Replies

5. Shell Programming and Scripting

Grep returns nothing

Hi all, I am trying to grep a .txt file for a word. When I hit enter, it returns back to $ The file is 4155402 in size and is named in this way: *_eveningtimes_done_log.txt I use this command, being in the same directory as the file: grep -i "invalid" *_eveningtimes_done_log.txt ... (16 Replies)
Discussion started by: DallasT
16 Replies

6. Shell Programming and Scripting

If the grep command returns any result set

my intension is to use a grep command inside the shell script and if any row is returned or not.. depending on the resultset i have to code on further. how to check this i mean.. could anyone help me out with the if condition how to use it here !! (4 Replies)
Discussion started by: gotam
4 Replies

7. Shell Programming and Scripting

find/grep returns no matches

Hi all! I've faced with very unintelligible error using find/grep like this: root@v29221:~# find /var/www/igor/data/www/lestnitsa.ru | grep u28507I get nothing as a result, but: root@v29221:~# grep u28507 /var/www/igor/data/www/lestnitsa.ru/_var.inc $db_name = 'u28507';... (2 Replies)
Discussion started by: ulrith
2 Replies

8. UNIX for Dummies Questions & Answers

Grep without returns...

Is there a command where I can pipe my grep into it and it will output it with spaces rather than returns? Example I want to turn prompt$ grep blah file blah blah into this prompt$ grep blah file | someCommand blah blah (1 Reply)
Discussion started by: mrwatkin
1 Replies

9. Shell Programming and Scripting

if returns "unknown test operator"

Greetings, using ksh on Solaris, I am trying to identify the current version of a package installed on multiple servers using if statement in a precursor to upgrading. I have searched the forums and have found many hits, reviewed 3 pages and have tried the different variations noted there. Also... (3 Replies)
Discussion started by: 22blaze
3 Replies

10. Shell Programming and Scripting

test: unknown operator status

hi I get test: unknown operator status if then echo "OK." return 0 else echo "not ok" 2>&1 exit -1 fi I tried to change "A" with 'A' --> same error I tried to change if , I am getting: (3 Replies)
Discussion started by: melanie_pfefer
3 Replies

Featured Tech Videos