Eric Saboia Eric Saboia - 5 months ago 9
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 http://www.tldp.org/LDP/intro-linux/html/intro-linux.html

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

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

echo "Linux Linux Linux" | grep -woc Linux
1

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
3