#!/usr/bin/env bash
# this is a script that counts the number of instances of
# a single word lyric in a song
if [ $# -ne 2 ]; then
echo "Usage: ./lyric_count word lyric_file" 1>&2
exit 1
fi
word="$1"
song="$2"
count=0
while read -r line; do
for lyric in $line; do
if grep -iq "$word" <<< "$lyric"; then
((count++))
fi
done
done < "$song"
echo "There are $count instances of '$word' in $song."
#!/usr/bin/env bash
#
# Demo using `grep` with the `-E` option.
# Search for lines containing the word "sample"
grep -E 'sample' sample.txt
# Search for lines starting with the word "It"
grep -E '^It' sample.txt
# Search for lines ending with the word "works!"
grep -E 'works!$' sample.txt
# Search for lines containing either "file" or "text"
grep -E 'file|text' sample.txt
# Search for lines containing the word "text" followed by zero or more characters
grep -E 'text.*' sample.txt
# Search for lines containing the word "sample" followed by any single character
grep -E 'sample.' sample.txt
# Search for lines containing some letters
grep -E '[a-zA-Z]' sample.txt