views:

77

answers:

5

I want to run a script in unix that will look for a specific pattern inside a passed argument. The argument is a single word, in all cases. I cannot use grep, as grep only works on searching through files. Is there a better unix command out there that can help me?

+4  A: 

Grep can search though files, or it can work on stdin:

$ echo "this is a test" | grep is
this is a test
atk
A: 

As of bash 3.0 it has a built in regexp operator =~

Checkout http://aplawrence.com/Linux/bash-regex.html

jamihash
+1  A: 

Depending on what you're doing you may prefer to use bash pattern matching:

# Look for text in $word
if [[ $word == *text* ]]
then
  echo "Match";
fi

or regular expressions:

# Check is $regex matches $word
if [[ $word =~ $regex ]]
then
  echo "Match";
fi
Alexandre Jasmin
+1  A: 

you can use case/esac as well. No need to call any external commands (for your case)

case "$argument" in
  *text* ) echo "found";;
esac
ghostdog74
A: 
if echo $argument | grep -q pattern
then
    echo "Matched"
fi
dogbane