Saturday, June 7, 2008

bash - search a letter in a string - awk


_____________________________________
Search a letter in a string using grep
_____________________________________



$ string="unix-bash 2389"

$ character="-"

$ echo $string | grep -q "$character" && echo "found" || echo "not found"
found

$ character="@"

$ echo $string | grep -q "$character" && echo "found" || echo "not found"
not found


_______________________________________
Search a letter in a string using awk
_______________________________________


$ character="@"

$ echo $string | awk -vc="$character" '{if(gsub(c,"")) print "Found";else print "Not Found"}'
Not Found

$ character="-"

$ echo $string | awk -vc="$character" '{if(gsub(c,"")) print "Found";else print "Not Found"}'
Found

1 comment:

Karan Bohra said...

__________________________________________
Search a letter in a string using builtins
__________________________________________

case $string in
*"$character"* ) echo "found" ;;
* ) echo "not found" ;;
esac

© Jadu Saikia www.UNIXCL.com