The UNIX and Linux Forums  

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
Grep command nickg UNIX for Dummies Questions & Answers 14 10-06-2008 08:22 AM
how to exclude the GREP command from GREP yamsin789 UNIX for Advanced & Expert Users 2 10-04-2007 11:59 PM
grep command help ishmael^soyuz Shell Programming and Scripting 4 07-11-2007 06:01 AM
grep command pmsuper UNIX for Dummies Questions & Answers 6 11-22-2006 04:12 AM
grep command debasis.mishra Shell Programming and Scripting 1 03-27-2006 10:53 PM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #8  
Old 05-01-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,650
You don't really want grep.

Code:
case $input in *[!0-9]*) echo Need a number >&2; exit 1;  esac
fabtagon's solution does not check the number of digits at all, it just checks that there is a number in there somewhere. But the thing you really want to check is that there is nothing there which is not a digit. You could do that with grep, but case is absolutely a better solution.
Reply With Quote
Forum Sponsor
  #9  
Old 05-01-2008
Registered User
 

Join Date: Apr 2008
Posts: 15
grep command

that code is really helpfull

i am just wondering if i want to check to number, is it possible to combine the code

example
case [ $input1 -a $input2 ] in
*[!1-9]*) echo not number
esac
Reply With Quote
  #10  
Old 05-01-2008
Registered User
 

Join Date: Jun 2006
Posts: 3
grepping for text

I wrote a utility menu to do this. Try some of this code:

# find file names containing text

1) echo ""
echo 'which dir or .?'
read dir
echo 'what text to find?'
read text
echo " do you want to put the findings to a file?"
read answer
if [ "$answer" = "y" ]
then
echo "outputting to text_found"
find $dir -exec grep -l $text {} \; > text_found;
echo "findings have been output to `pwd`/text_found"
echo "would you like to view? y/n"
read answer
if [ "$answer" = "y" ]
then
more text_found
fi
else
echo ""
find $dir -exec grep -l $text {} \; 2>/dev/null|more
fi
;;
Reply With Quote
  #11  
Old 05-05-2008
Registered User
 

Join Date: Apr 2008
Posts: 15
grep command

that is so helpfull, thanks for the help
Reply With Quote
  #12  
Old 05-05-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,650
Quote:
Originally Posted by christine33990 View Post
i am just wondering if i want to check to number, is it possible to combine the code

example
case [ $input1 -a $input2 ] in
*[!1-9]*) echo not number
esac
Yes, the syntax you want is quite different though.

[ is a separate command (believe it or not!) and is customarily only used as an argument to if and perhaps occasionally while; case simply looks at the next token and compares it. But you can do two comparisons:

Code:
case $input1 in *[!1-9]*) echo not number;; esac
case $input2 in *[!1-9]*) echo not number;; esac
In fact, you could check them both in one go, if you don't care which one is not correct:

Code:
case "$input1$input2" in *[!1-9]*) echo not number;; esac
Sorry for taking so long to respond, I had this open in my browser but got interrupted ...

The earlier example I posted lacked the double semicolons; sorry for omitting them (though some shells can apparently cope if it's in the last branch in a case structure).

Last edited by era; 05-05-2008 at 11:47 PM. Reason: Missing double semicolons in earlier reply
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 08:15 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0