Search
 
SCRIPT & CODE EXAMPLE
 

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
Comment

grep substring

# You can do it in two ways
# 1) Let "grep" read on its standard input

echo "$line" | grep -o select

# 2) tell "grep" here is the string

grep select <<< "$line"
Comment

grep for substring

output=$(command)
[[ $output =~ (CpuIowait=[0-9][.][0-9]{2}) ]] && echo "${BASH_REMATCH[1]}"
Comment

PREVIOUS NEXT
Code Example
Shell :: pacman command on arch 
Shell :: bash search history 
Shell :: take a screenshot linux terminal 
Shell :: scp command from local to remote 
Shell :: installing helm on linux 
Shell :: blue ocean jenkins 
Shell :: how to get list folder in cmd to value 
Shell :: how to use nano command in linux 
Shell :: kubernetes setup 
Shell :: bash list columns 
Shell :: packet tracer 2.7.1 Full Config P2P-3 Router 
Shell :: How to clean up the git repo and reduce its disk size 
Shell :: linux shutdown no password 
Shell :: repo tool depth 
Shell :: git cache ssh password 
Shell :: hanselman pretty prompt 
Shell :: The following signatures were invali gpg 
Shell :: ssh change pasword to key file 
Shell :: check if string in file 
Shell :: Error: No Homebrew ruby 2.6.8 available for arm64 processors! 
Shell :: delete all view options mac os 
Shell :: dirname=/usr/bin in linux 
Shell :: install gearman di linux 
Shell :: how to clear log snap.io 
Shell :: upgrade spyer 4.2.0 in anaconda 
Shell :: how many ppi is 4k 
Shell :: pip install qiskit does not work 
Shell :: crate db helm 
Shell :: iris eyesaver 
Shell :: linux aarch64 arm none eabi cross compiler ubuntu 
ADD CONTENT
Topic
Content
Source link
Name
9+9 =