To have a numeric result from grep


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers To have a numeric result from grep
# 1  
Old 08-07-2006
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  
Old 08-07-2006
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  
Old 08-07-2006
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.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep string with regex numeric characters

Hi all, I have the following entries in a file: Cause Indicators=80 90 Cause Indicators=80 90 Cause Indicators=82 90 Cause Indicators=82 90 Cause Indicators=82 90 The first 2 digits might change so I am after a sort of grep which could find any first 2 digits + the second 2,... (3 Replies)
Discussion started by: nms
3 Replies

2. Shell Programming and Scripting

Grep result from dd command

Hi, I am running following command in a bash script for testing IO and use grep to get throughput number, but it did not work, it displayed everything: dd if=/dev/zero of=/dev/null bs=1G count=1 oflag=dsync | grep bytes | awk '{print $7}' 1+0 records in 1+0 records out 536870912 bytes... (2 Replies)
Discussion started by: hce
2 Replies

3. UNIX for Dummies Questions & Answers

Bash - CLI - grep - Passing result to grep through pipe

Hello. I want to get all modules which are loaded and which name are exactly 2 characters long and not more than 2 characters and begin with "nv" lsmod | (e)grep '^nv???????????? I want to get all modules which are loaded and which name begin with "nv" and are 2 to 7 characters long ... (1 Reply)
Discussion started by: jcdole
1 Replies

4. Solaris

grep result in newline

Hi While trying to do a search on solaris, the grep results seems to be appearing on the same line instead of the new line. Wed Jan 18 14:45:48 weblogic@test:/abcd$ grep qainejb02 * qa_cluster_biz_view_tc_intl_servers_ports_2:qainejb02 7101 qa_cluster_servers_2:qainejb02... (2 Replies)
Discussion started by: ganga.dharan
2 Replies

5. UNIX for Dummies Questions & Answers

grep, expecting 1 result, getting more

Hi Please take a look below, I'm grepping for /app/oracle and would like explicitly that result and not /app/oracle/admin as well. # cat /tmp/fs.list /app/oracle /app/oracle/admin # cat /tmp/fs.list | grep -w "/app/oracle" /app/oracle /app/oracle/admin (3 Replies)
Discussion started by: s1ckle
3 Replies

6. Shell Programming and Scripting

how to grep only particular length of numeric values

hi i have two types of file 1. temp.0000000001.data (10 digit numeric) 2. temp.000000001.data (9 digit numeric) i want to search a file which is having 10 digit numeric in between the file name. i use command like this.. ls | grep temp.^*.data but this will give both the files as... (2 Replies)
Discussion started by: somi2yoga
2 Replies

7. Shell Programming and Scripting

pipe result from grep

Trying to create a command line script to look for all files matching a pattern, grep for a specific value in each file, and write out the filename long list. It's possible the filename won't containe the value. { echo “Running....” for fname in 811_Intermediate_File_* do grep -l... (3 Replies)
Discussion started by: gavineq
3 Replies

8. Shell Programming and Scripting

How to negate grep result?

Here is my script so far: set dirs = ` find . -name "message.jar" 2> /dev/null | cut -d "/" -f 2 ` | uniq foreach dir ( $dirs ) if (grep $dir/* someText==null) --> how do I write this in script? print $dir end end (4 Replies)
Discussion started by: mmdawg
4 Replies

9. Shell Programming and Scripting

diaplaying the grep result

Hi, My code is like this if swlist -a revision 2>/dev/null | grep ABC 2>/dev/null then echo "Found Above mentioned ABC Version, please remove it first..." fi This is displaying the result to the screen. i want to first suppress that and for that i wrote the below... (1 Reply)
Discussion started by: rag84dec
1 Replies

10. UNIX for Dummies Questions & Answers

grep to handle a 0 result

Hi guys, I have the following grep command in a script to search through a file for a string and return its count, and it works fine for when the string exists: grep "string" file.txt | wc However, sometimes the result will be 0 and I want the script to take this as the result. Right now... (6 Replies)
Discussion started by: ocelot
6 Replies
Login or Register to Ask a Question