Three are 2 sets of regular expression format in unix world:
BRE: Basic Regular Expressions
ERE: Extended Regular Expressions
grep is using BRE, egrep is using ERE, fgrep is fixed string match.
Latest POSIX standard merged BRE,ERE,fgrep into grep, with different options, in Ubuntu Linux:
grep -G : basic regular expressions(BRE), this is default
grep -E : extended regular expressions(ERE)
grep -F : fixed string match
2. Use grep:
test:
#! /bin/bash
num_users #output command not found, since
echo $PATH
#output: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
PATH=$PATH:$HOME/Desktop/xxdev #Add current path to $PATH
echo $PATH #output normal path string + current path
#output: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/home/aubinxia/Desktop/xxdev
num_users # ok to run now
#output:
#+ who
#+ wc -l
#2
#+ set +x
Use BRE(List all lines containing PATH in file ./test)
aubinxia@aubinxia-VirtualBox:~/Desktop/xxdev$ grep PATH ./test
echo $PATH
PATH=$PATH:$HOME/Desktop/xxdev #Add current path to $PATH
echo $PATH #output normal path string + current path
Use ERE, and fixed string:
aubinxia@aubinxia-VirtualBox:~/Desktop/xxdev$ grep -E PATH ./test
echo $PATH
PATH=$PATH:$HOME/Desktop/xxdev #Add current path to $PATH
echo $PATH #output normal path string + current path
aubinxia@aubinxia-VirtualBox:~/Desktop/xxdev$ grep -F PATH ./test
echo $PATH
PATH=$PATH:$HOME/Desktop/xxdev #Add current path to $PATH
echo $PATH #output normal path string + current path
Use Other options:
-q: quiet, output nothing:
aubinxia@aubinxia-VirtualBox:~/Desktop/xxdev$ grep -q PATH ./test
aubinxia@aubinxia-VirtualBox:~/Desktop/xxdev$
-l: list all files, instead of outputting the line itself
aubinxia@aubinxia-VirtualBox:~/Desktop/xxdev$ grep -l PATH ./*
./test
-v: list all lines which don't match the pattern:
aubinxia@aubinxia-VirtualBox:~/Desktop/xxdev$ grep -v PATH ./test
#! /bin/bash
num_users #output command not found, since
#output: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
#output: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/home/aubinxia/Desktop/xxdev
num_users # ok to run now
#output:
#+ who
#+ wc -l
#2
#+ set +x
Combine several options:
Use ERE, and only list files themselves:
aubinxia@aubinxia-VirtualBox:~/Desktop/xxdev$ grep -lE PATH ./test
./test
No comments:
Post a Comment