Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR SHELL

grep substring in bash

# 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
Source by unix.stackexchange.com #
 
PREVIOUS NEXT
Tagged: #grep #substring #bash
ADD COMMENT
Topic
Name
5+8 =