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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to scan a file for literal, return 0 or 1 if found?
# 1  
Old 07-31-2007
How to scan a file for literal, return 0 or 1 if found?

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
# 2  
Old 07-31-2007
Code:
egrep "error|ERROR" lpcmp165.out 1>/dev/null 2>&1
mRetCode=$?
if [ $mRetCode -eq 0 ]; then
  echo 'Found'
else
  echo 'Not found'
fi

# 3  
Old 07-31-2007
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
# 4  
Old 07-31-2007
this site should hire you full time or set up some kind of Kudo's or donate button!
# 5  
Old 07-31-2007
Thank you for your kind words, BobK. Smilie

Our satisfaction is to see your problems resolved.

Take care!
# 6  
Old 07-31-2007
ignore case -i option with grep

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

# 7  
Old 07-31-2007
Quote:
Originally Posted by matrixmadhan
Code:
if [ `grep -i error filename 1>/dev/null 2>&1` -eq 0 ]
then
echo "found"
else
echo "not found"
fi

I think you meant

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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Fgrep literal string from a file

have a file1 aaa-bbb-ccc-abcd aaa-bbb-ccc-bacd aaa-bbb-ccc-aaad aaa-bbb-ccc-ahave another file2 aaa-bbb-ccc-a fileusing the fgrep command, trying to have only the literal string returned. fgrep -f file2 file1 is returning aaa-bbb-ccc-abcd aaa-bbb-ccc-aaad aaa-bbb-ccc-aOnly looking for... (1 Reply)
Discussion started by: jimmyf
1 Replies

2. Shell Programming and Scripting

Return first two columns if match found among two files

Hi, I have FileA with one column. File B with 15 columns separated by comma delimiter. I need to compare the FILEA value with all 15 columns of FILEB... if matches, need to return the 1st, 2nd column values of FILEB. How to achieve this through shell script? Thanks in advance. (5 Replies)
Discussion started by: vamsikrishna928
5 Replies

3. UNIX for Dummies Questions & Answers

How to find a file based on pattern & return the filename if found?

Hi all, I am a newbie here. I have this requirement to find a file based on a pattern then return the filename if found. I created a script based on online tutorials. Though, I am stuck & really appreciate if anyone can have a quick look & point me to the right direction? #Script starts... (10 Replies)
Discussion started by: buster_t
10 Replies

4. Shell Programming and Scripting

Avoid carriage return until ^M is found (CentOS 6, bash 4.1)

Hi everyone, I have the following contents in a text file (as seen when viewed using vim): one two three ^M four five six ^M seven eight nine ^M ten eleven twelve ^M (That is just a small portion of the file) How can I obtain the following result? one two three ^M four five six ^M seven... (2 Replies)
Discussion started by: gacanepa
2 Replies

5. Shell Programming and Scripting

awk Help -- If match found return the count

Hi All, I need to get the count of records in the file, if the passing parameter matches with the list of records in the file. Below is my example source file: Test1.dat 20120913 20120913 20120912 20120912 20120912 20120912 20120912 20120913 20120913 20120912 In my script I am... (5 Replies)
Discussion started by: bbc17484
5 Replies

6. Shell Programming and Scripting

bash: need to have egrep to return a text string if the search pattern has NOT been found

Hello all, after spending hours of searching the web I decided to create an account here. This is my first post and I hope one of the experts can help. I need to resolve a grep / sed / xargs / awk problem. My input file is just like this: ----------------------------------... (6 Replies)
Discussion started by: bash4ever
6 Replies

7. Shell Programming and Scripting

Bash: Reading out rows of a file into a dynamic array and check first literal

Hello, i have a file "Movie.ini" looking e.g. like follows * MOVIE A bla bla MOVIE B blubb blubb MOVIE C I'd like to read the file "Movie.ini" with cat and grep and check whether it includes the string MOVIE only with a '*' at the beginnig. By doing "cat Movie.ini| grep MOVIE... (14 Replies)
Discussion started by: ABE2202
14 Replies

8. Shell Programming and Scripting

Scripting problem - when the file is not found i want it to return to the menu

when the file is not found i want it to return to the menu, however it carries out the next line when i hit a key I know its probably something simple can anyone help? here is my pause function: function pause(){ read -s -n 1 -p "Press any key to return to Menu . . ." echo } SCRIPT... (2 Replies)
Discussion started by: Alendrin
2 Replies

9. Shell Programming and Scripting

Return a message when a file is not found

Hi there, I am writing a script to look for tmp log files that have not been access within the last 10 days. I am using the follwing command within the script: find /var/tmp -name *log -atime -9 ¦xargs What I would like to be able to do would be to display a message if there is no... (3 Replies)
Discussion started by: lodey
3 Replies

10. Shell Programming and Scripting

File Scan

Hi everyone , i m working on Sun solaris and i have a file "smsapp.cur" which has information like this paragraph given below , there are millions of such paragraphs From:923212802736 To:923222326807 logMessage: 07-04-08 17:34:29 Getting message topup from code page default in language English... (2 Replies)
Discussion started by: Dastard
2 Replies
Login or Register to Ask a Question