The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to negate grep result? mmdawg Shell Programming and Scripting 4 05-05-2008 08:24 AM
diaplaying the grep result rag84dec Shell Programming and Scripting 1 03-27-2008 02:37 AM
append a string to a grep result melanie_pfefer Shell Programming and Scripting 8 03-19-2008 07:19 AM
grep to handle a 0 result ocelot UNIX for Dummies Questions & Answers 6 02-05-2007 11:19 AM
is there any why to get the number of line in grep result ? umen UNIX for Dummies Questions & Answers 1 03-16-2006 09:56 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 08-07-2006
Hak Dee Hak Dee is offline
Registered User
  
 

Join Date: Aug 2006
Posts: 1
To have a numeric result from grep

I am new to unix. i need to know how to use grep to grep and expression from a file. and pass the result as a 0 for found and 1 for not found. I can only go up to grep 'Checking Subscription Status' ranos.log. Please help. Thank you.
  #2 (permalink)  
Old 08-07-2006
Dhruva's Avatar
Dhruva Dhruva is offline
Registered User
  
 

Join Date: Mar 2006
Location: India
Posts: 255
let's take it with example
suppose there is a file "sample.txt" with contents
1c1 Berger Awc ANP20070201301 4000.50
1c2 Bose W G ANP20070201609 6000.70
1c2 Andy CK ANP20070201230 28000.00
1c3 Bharg ANP20070201847 9000.80

and you want to search for a string "Berger".The following command will give value 0 in X if patern found else 1.

Code:
grep -i "berger" sample.txt>/dev/null
X=`echo $?`
  #3 (permalink)  
Old 08-07-2006
blowtorch's Avatar
blowtorch blowtorch is offline Forum Advisor  
Supporter
  
 

Join Date: Dec 2004
Location: Singapore
Posts: 2,350
You don't have to worry about converting the result into 0 for found and 1 otherwise. The shell takes care of this for you.
Check the status like this:
Code:
grep 'Checking Subscription Status' ranos.log > /dev/null
if [ $? -eq 0 ]; then
   echo success!
else
   echo failure!!
fi
All commands have a return code that is returned to the sh/ksh/bash family of shells as the variable $? (grep will return 0 if it finds what it is looking for). You can just run the return code via an if, and get your result.
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 11:58 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0