cat txt no is ok so is ok ok is no so it is ok you are no so he is no so 12 is good 39 + 45 = 84 this is bad weather sono is bad boy.
(1). grep 形式:
echo "20121212"|grep "[0-9]\{4\}[0-9]\{2\}[0-9]\{2\}":其中,\{ --->是对{ 转义字符; 20121212 注:在正则表达式中 一些通配符如果出现在表达式中,必须用\ 进程转义,才表示原本的意思。(2). awk 形式:
awk '/^(no|so)/' txt #打印以 no 或so 开头的所有行 awk '/^[0-9]\{4\}*\.bat/{print $1} txt #打印以4个0-9数字开头,以 .bat 结尾模式的行的第一个字段
awk '$1 ~/^[0-9][/0-9]/{print $0}' txt # txt 文件每条记录的$1 如果是以2个 [0-9] 开始 12 is good 39 + 45 = 84 awk '$1 !~/^[0-9][/0-9]/{print $0}' txt no is ok so is ok ok is no so it is ok you are no so he is no so this is bad weather sono is bad boy.
2. shell 的 正则表达式 基础知识【good】
http://blog.csdn.net/fitywang/article/details/2156089 -->花时间看的资料
http://bbs.chinaunix.net/thread-2110174-1-1.html
4. sed 正则表达式
http://blog.sina.com.cn/s/blog_807992170100sy95.html
5. shell常用正则表达式
http://www.blogjava.net/jasmine214--love/archive/2011/10/12/361040.html
6. grep -P '' txt P选项是对 \t 这样的特殊字符生效:
cat t aaa bbb aaa ccc aaa ddd
grep '\t' t $ grep -P '\t' t aaa bbb aaa ddd
grep -n txt -n:参数是显示匹配行的前后n行 -A2 -B2
cat txt aaa 111 bbb ccc 222 ddd eee 333 ggg jjj 444 kkk hhh 555 lll mmm 666 nnn
匹配行的前2行:
grep jjj -B2 txt ccc 222 ddd eee 333 ggg jjj 444 kkk匹配行的后2行:
jjj 444 kkk hhh 555 lll mmm 666 nnn
grep jjj -2 txt ccc 222 ddd eee 333 ggg jjj 444 kkk hhh 555 lll mmm 666 nnn