Eric Saboia Eric Saboia - 1 year ago 63
Bash Question

How do I search a word in Shell Script (Bash)?

I need to search the string "Linux" in a web page, first in case-sensitive, sencond in case-insensitive.

Well, my search will be in

I made the bellow code, but it is not return every words "Linux".

echo Linux: $(grep -wc "Linux" ~/workspace/ep-exercicios/m1/e2/intro-linux.html)

For example, my code return 405 words, but has 447.

How do it?

Answer Source

grep counts matching lines, so lines with multiple matches are counted only once.

echo "Linux Linux Linux" | grep -woc Linux

You could use the -o flag to generate one line per match, then wc -l to count

echo "Linux Linux Linux" | grep -o Linux | wc -l