|
grep 命令
功能:分析一行信息,若该行中有我们需要的信息,就将该行整行拿出来
grep 进阶选项:
dmesg 可列出核心产生的讯息
dmesg | grep -n –color=auto ‘qxl’
dmesg | grep -n -A3 -B2 –color=auto ‘qxl’
grep --color=auto ‘qxl’ etc/man/zzq/band
基础正规表达法练习
1.
grep -n --color=auto “the” 1.txt #将1.txt中含有the的行输出出来;
grep -vn --color=auto “the” 1.txt #反向选择-v,不输出含有the的行
grep -in --color=auto “the” 1.txt #不区分大小写-i,输出含有the的行,不区分大小写;
2. 利用中括号[ ]来搜索集合字符
grep -n --color=auto ‘t[ae]st’ 1.txt #检索含有tast和test的字符
grep -n --color=auto ‘oo’ 1.txt #搜索含有oo字符的行;
3. [^] ,代表反向选择
grep -n --color=auto ‘[^g]oo’ 1.txt #搜索oo字符前面没有g的行,[^],代表反向选择;
grep -n --color=auto ‘[^a-z]oo’ 1.txt #输出字符oo前面没有小写字母的行
grep -n --color=auto ‘[^A-Z]oo’ 1.txt #输出字符oo前面没有大写字母的行
grep -n --color=auto ‘[^0-9]oo’ 1.txt #输出字符oo前面没有字母的行
如果字符组是连续的可以使用-来代表连续字符组,如[a-z],[A-Z],[0-9]
grep -n --color=auto ‘[a-zA-Z0-9]’ 1.txt #检索具有数字和字母的行;
grep -n --color=auto ‘[^[ : lower: ] ]’ 1.txt #输出不含有小写字母的行
[ : lower: ]代表a-z的意思;
4. 行首与行尾字符^ $
行首字符^
注意区分^与[^],^在括号外代行首字符,在括号内代表反向选择
grep -n ‘^the’ 1.txt #^代表行首字符,该命令只提取行首为the的行
grep -n ‘^[a-z]’ 1.txt #只提取行首为小写字母的行;
grep -n ‘^[ [ : lower: ] ]’ 1.txt #与上一条命令等价;
grep -n ‘^[^a-zA-Z]’ 1.txt #只提取行首为非英文字母的行;
行尾字符$
grep -n ‘\ .$’ 1.txt #找出行尾结束字符为小数点的那一行;
因为小数点具有其他特殊意义,所以必须使用跳脱字符\来解释其意义
grep -n ‘^$’ 1.txt4 #因为只有行尾与行首,所以可以找出空白行
grep -v ‘^$’ 1.txt | grep -v ‘^#” 1.txt #代表不输出空白行和不输出开头是#的行
任意一个字符 . 与重复字符 *
grep -n ‘g..d’ 1.txt #找出含有以g开头,以d结尾,g、d之间含有任意两个字符的行
grep -n ‘ooo*’ 1.txt #找出至少含有两个oo字符串的行;