How Do You Parse a String in Bash?

Problem scenario
You want to count the number of "(" (open parens) in a string. How do you parse a string in Bash?

Solution
Remember to use awk. The -F flag can allow you to designate a character to count and the NF flag can count the occurrences.

echo "continualintegration" | awk -F "o" '{print NF-1}'

echo "c(ntinua((lintegrati(n" | awk -F "(" '{print NF-1}'

You may also want to see this posting parse an array.

Leave a comment

Your email address will not be published. Required fields are marked *