Answers for "shell: how to grep on a variable and count the number of occurences"

0

shell: how to grep on a variable and count the number of occurences

# It can be done in two ways
# 1) Have "grep" read on from the standard input using a pipe
#    and search the input string. Then pipe the result to "wc" to count 
#    the number of occurences

$ line="This is where we select from a table."
# substr="select"

$ echo "$line" | grep "$substr" | wc -l

# 2) or pass a string to "grep" and search the string for a substring
#    pass the result to "wc" to count the number of occurence

$ grep "$substr" <<< "$line" | wc -l
Posted by: Guest on April-20-2022

Code answers related to "shell: how to grep on a variable and count the number of occurences"

Code answers related to "Shell/Bash"

Browse Popular Code Answers by Language