标签:
# grep match_pattern filename //将会输出符合match_pattern规定的内容,match pattern为通配符
# grep -E match_pattern filename //这里的match_pattern为正则表达式
# grep -o -E match_pattern filename //只输出匹配的部分
# grep -v match_pattern filename //包含math_pattern匹配的行之外的行
# grep -c "text" filename //匹配字符串的行数
# egrep -o match_pattern | wc -l //字符串出现的次数
# grep "text" . -R -n //递归搜索文件
# grep -e match_pattern -e match_pattern //匹配多个样式
# grep -f pattern_file source_filename //匹配多个样式,样式以文件的形式出现
# grep match_pattern -A 3 //某个结果之后的3行
# grep match_pattern -B 3 //某个结果之前的3行
# grep match_pattern -C 3 //某个结果的前后3行
# grep linux -n sample.txt
1:gnu is not linux
2:linux is fun
# grep linux -n sample.txt sample2.txt
sample.txt:1:gnu is not linux
sample.txt:2:linux is fun
sample2.txt:4:planetlinux
# egrep "[i|s]+ " sample.txt
gnu is not linux
linux is fun
bash is art
# egrep "[i|s]+ " -b -o sample.txt
4:is
23:is
35:is
# grep -l linux sample.txt sample2.txt
sample.txt
sample2.txt
# grep -L linux sample.txt sample2.txt
# grep "linux" . -R -n
./sample2.txt:4:planetlinux
./data.txt:4:4 linux 1000
./newDir/sample.txt:1:gnu is not linux
./newDir/sample.txt:2:linux is fun
./sample.txt:1:gnu is not linux
./sample.txt:2:linux is fun
# grep -i "LINUX" sample.txt
gnu is not linux
linux is fun
# grep -e "gun" -e "linux" sample.txt
gnu is not linux
linux is fun
# grep "hello" . -r --include *.txt
out.txt:hello
# grep "hello" . -r --exclude "*.txt"
./test:hello
#!/bin/bash if [ $# -ne 2 ]; then echo "$0 match_text filename" fi match_text=$1 filename=$2 grep -q $match_text $filename if [ $? -eq 0 ]; then echo "the text exsit" else echo "text doesn‘t exsit" fi
# ./silent_grep.sh student stu_data.txt
the text exsit
标签:
原文地址:http://www.cnblogs.com/KarenWang/p/4555406.html