# How to scan a file for literal, return 0 or 1 if found?

**URL:** <https://community.unix.com/t/how-to-scan-a-file-for-literal-return-0-or-1-if-found/173749>\
**Category:** Shell Programming and Scripting\
**Created:** [July 31, 2007, 11:23am UTC](https://community.unix.com/t/how-to-scan-a-file-for-literal-return-0-or-1-if-found/173749 "2007-07-31T11:23:49Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![bobk544](https://community.unix.com/letter_avatar/bobk544/32/5_5575768a8748004e209b776fc1b2916d.png) [@bobk544](https://community.unix.com/u/bobk544)\
**Post date:** [July 31, 2007, 11:23am UTC](https://community.unix.com/t/how-to-scan-a-file-for-literal-return-0-or-1-if-found/173749/1 "2007-07-31T11:23:49Z")

</div>

How can i scan a file in a UNIX script and look for a particular keyword?

For example if i wanted to scan the file "lpcmp165.out" and see if it contains the term "error" or "ERROR" and then return a 0 or 1 or some indicator as such?

Detail example:  
sqlplus -s xx/yyyyyyy#@zzz \<\<EOF \> lpcmp165.out  
set pagesize 0 feedback off verify off heading off echo off  
set serveroutput on  
DROP INDEX PKM\_TCQ\_DT\_TPD\_LOAN\_EXLOAN\_ID;  
commit;  
exit;  
EOF

if [lpcmp165.out contains "error"] ; then \<\<\<\<\< psuedo code  
echo "bad result"  
else  
echo "good result"  
fi

lpcmp165.out contains this output:  
DROP INDEX xPKM\_TCQ\_DT\_TPD\_LOAN\_EXLOAN\_ID  
\*  
ERROR at line 1:  
ORA-01418: specified index does not exist

---

<div class="post-metadata">

**Author:** ![Shell\_Life](https://community.unix.com/user_avatar/community.unix.com/shell_life/32/521_2.png) [@Shell\_Life](https://community.unix.com/u/Shell_Life)\
**Post date:** [July 31, 2007, 11:40am UTC](https://community.unix.com/t/how-to-scan-a-file-for-literal-return-0-or-1-if-found/173749/2 "2007-07-31T11:40:23Z")

</div>

```nohighlight
egrep "error|ERROR" lpcmp165.out 1>/dev/null 2>&1
mRetCode=$?
if [$mRetCode -eq 0]; then
  echo 'Found'
else
  echo 'Not found'
fi

```

---

<div class="post-metadata">

**Author:** ![bobk544](https://community.unix.com/letter_avatar/bobk544/32/5_5575768a8748004e209b776fc1b2916d.png) [@bobk544](https://community.unix.com/u/bobk544)\
**Post date:** [July 31, 2007, 11:53am UTC](https://community.unix.com/t/how-to-scan-a-file-for-literal-return-0-or-1-if-found/173749/3 "2007-07-31T11:53:39Z")

</div>

wow! thanks again ShellLife! works great!

can't say enough how helpful you've been over time, saved me many hours!

have a great week!

BobK

---

<div class="post-metadata">

**Author:** ![bobk544](https://community.unix.com/letter_avatar/bobk544/32/5_5575768a8748004e209b776fc1b2916d.png) [@bobk544](https://community.unix.com/u/bobk544)\
**Post date:** [July 31, 2007, 12:07pm UTC](https://community.unix.com/t/how-to-scan-a-file-for-literal-return-0-or-1-if-found/173749/4 "2007-07-31T12:07:07Z")

</div>

this site should hire you full time or set up some kind of Kudo's or donate button!

---

<div class="post-metadata">

**Author:** ![Shell\_Life](https://community.unix.com/user_avatar/community.unix.com/shell_life/32/521_2.png) [@Shell\_Life](https://community.unix.com/u/Shell_Life)\
**Post date:** [July 31, 2007, 1:43pm UTC](https://community.unix.com/t/how-to-scan-a-file-for-literal-return-0-or-1-if-found/173749/5 "2007-07-31T13:43:13Z")

</div>

Thank you for your kind words, BobK. 🙂

Our satisfaction is to see your problems resolved.

Take care!

---

<div class="post-metadata">

**Author:** ![matrixmadhan](https://community.unix.com/letter_avatar/matrixmadhan/32/5_5575768a8748004e209b776fc1b2916d.png) [@matrixmadhan](https://community.unix.com/u/matrixmadhan)\
**Post date:** [July 31, 2007, 2:05pm UTC](https://community.unix.com/t/how-to-scan-a-file-for-literal-return-0-or-1-if-found/173749/6 "2007-07-31T14:05:04Z")

</div>

ignore case -i option with grep

```nohighlight
if [`grep -i error filename 1>/dev/null 2>&1` -eq 0]
then
echo "found"
else
echo "not found"
fi

```

---

<div class="post-metadata">

**Author:** ![kahuna](https://community.unix.com/user_avatar/community.unix.com/kahuna/32/541_2.png) [@kahuna](https://community.unix.com/u/kahuna)\
**Post date:** [July 31, 2007, 2:33pm UTC](https://community.unix.com/t/how-to-scan-a-file-for-literal-return-0-or-1-if-found/173749/7 "2007-07-31T14:33:36Z")

</div>

> [@matrixmadhan](#):
>
> ```plaintext
> if [`grep -i error filename 1>/dev/null 2>&1` -eq 0]
> then
> echo "found"
> else
> echo "not found"
> fi
> 
> ```

I think you meant

```plaintext
if grep -i error filename 1>/dev/null 2>&1
then
echo "found"
else
echo "not found"
fi

```

---

<div class="post-metadata">

**Author:** ![matrixmadhan](https://community.unix.com/letter_avatar/matrixmadhan/32/5_5575768a8748004e209b776fc1b2916d.png) [@matrixmadhan](https://community.unix.com/u/matrixmadhan)\
**Post date:** [July 31, 2007, 2:52pm UTC](https://community.unix.com/t/how-to-scan-a-file-for-literal-return-0-or-1-if-found/173749/8 "2007-07-31T14:52:13Z")

</div>

> [@kahuna](#):
>
> I think you meant
> 
> ```plaintext
> if grep -i error filename 1>/dev/null 2>&1
> then
> echo "found"
> else
> echo "not found"
> fi
> 
> ```

No !

with this

```plaintext
`grep -i error filename 2>/dev/null 1>&2` -eq 0

```

the return value of grep command is directly equated to the value 0

o - success pattern has been found  
other value - pattern not found

---

<div class="post-metadata">

**Author:** ![Shell\_Life](https://community.unix.com/user_avatar/community.unix.com/shell_life/32/521_2.png) [@Shell\_Life](https://community.unix.com/u/Shell_Life)\
**Post date:** [July 31, 2007, 3:11pm UTC](https://community.unix.com/t/how-to-scan-a-file-for-literal-return-0-or-1-if-found/173749/9 "2007-07-31T15:11:06Z")

</div>

Matrix,  
Your solution:

```nohighlight
if [`grep -i error filename 1>/dev/null 2>&1` -eq 0]

```

does not work.

Kahuna is right in fixing your 'if' statement:

```nohighlight
if grep -i error filename 1>/dev/null 2>&1

```

It is important to note that the option '-i' does not work as it  
greps for more than just 'error' and 'ERROR':  
'Error', 'eRROR', 'ErRoR', or any other combination of mixed cases.

---

<div class="post-metadata">

**Author:** ![kahuna](https://community.unix.com/user_avatar/community.unix.com/kahuna/32/541_2.png) [@kahuna](https://community.unix.com/u/kahuna)\
**Post date:** [July 31, 2007, 3:27pm UTC](https://community.unix.com/t/how-to-scan-a-file-for-literal-return-0-or-1-if-found/173749/10 "2007-07-31T15:27:54Z")

</div>

> [@matrixmadhan](#):
>
> No !  
> the return value of grep command is directly equated to the value 0  
> o - success pattern has been found  
> other value - pattern not found

The code does not work as written. Please try to run it. The return value from grep will not be used in the if statement. Perhaps you meant to add a "-c"?

---

<div class="post-metadata">

**Author:** ![matrixmadhan](https://community.unix.com/letter_avatar/matrixmadhan/32/5_5575768a8748004e209b776fc1b2916d.png) [@matrixmadhan](https://community.unix.com/u/matrixmadhan)\
**Post date:** [August 2, 2007, 2:32pm UTC](https://community.unix.com/t/how-to-scan-a-file-for-literal-return-0-or-1-if-found/173749/11 "2007-08-02T14:32:01Z")

</div>

I accept ! 🙂

```nohighlight
cat test2
pattern

```

```nohighlight
grep -i pattern test2
if [$? -eq 0]
then
  echo "found"
else
  echo "not found"
fi

```

```nohighlight
if grep pattern test2 1>/dev/null 2>&1
then
  echo "found"
else 
  echo "not found"
fi

```

based on success or failure of the grep command

---

<div class="post-metadata">

**Author:** ![jim\_mcnamara](https://community.unix.com/letter_avatar/jim_mcnamara/32/5_5575768a8748004e209b776fc1b2916d.png) [@jim\_mcnamara](https://community.unix.com/u/jim_mcnamara)\
**Post date:** [August 2, 2007, 4:04pm UTC](https://community.unix.com/t/how-to-scan-a-file-for-literal-return-0-or-1-if-found/173749/12 "2007-08-02T16:04:56Z")

</div>

FWIW:  
use grep -q to suppress output:

```nohighlight
grep -q 'some string' myfile ; echo $(( ! $? ))

```

will show 1 if found and 0 if not found - ksh and bash.
