With a little side of applesauce...

Monday, January 29, 2007

BASH - Count occurrences of character in a string

Let’s say we want to find out how many forward slashes are found in a URL string:

URL=”www.example.com/test”

URL2=”www.example.com”

URLSLASH=$(echo “$URL” | grep -o “/” | wc -l | sed s/\ //g)

URL2SLASH=$(echo “$URL2″ | grep -o “/” | wc -l | sed s/\ //g)

echo “$URLSLASH”

echo “$URL2SLASH”

$ runscript.sh

1

0

I don’t know if you need the sed command at the end…

stolen from http://forums.macosxhints.com/archive/index.php/y-46934.html

No comments: