Yogsototh

bash (or zsh) regular expression matching

Posted in geek, script by yogsototh on November 28, 2007

I wanted to be able to write conditionnal on string matching in bash. One easy way to do that is :

if echo "$fic" | egrep "regexp" > /dev/null; then
    echo "it matches !"
else
    echo "it doesn't matches"
fi

But I found it a litle too long. This why I have written the small script match :

echo $1 | egrep $2 > /dev/null

in order to be able to write the more readable (in my point of vue) command :

if match "$fic" "regexp"; then
    echo "it matches !"
else
    echo "it doesn't matches"
fi
Tagged with: , , ,

One Response

Subscribe to comments with RSS.

  1. Lukas said, on September 11, 2008 at 1:12 pm

    Thank you for the long version!!! It really saved my holidays, couse I had to write a script in zsh that parsed a file and nothing seems to work, expect your way to use regex.

    Best regards. Lukas


Leave a Reply