7.grep和正则

来源:互联网 发布:一度教育java 编辑:程序博客网 时间:2024/06/04 20:00
1.grepgrep  [options] PATTERN [FILE …]   -i   —-color   -v:显示没有被模式匹配的字符串   -o:只显示被模式匹配到的字符串alias  grep=‘grep —color’*:任意长度的任意字符?:任意单个字符[][^]正则  regular  expression ,regex元字符:. :匹配任意单个字符[]:匹配指定范围内的任意单个字符[^]:匹配指定范围内的任意单个字符[[:digit:]] 数字grep ‘[[:digit:]]’ /etc/inittab   //以数字结尾的行[[:space:]]   空格[:lower:][:upper:][:punch:][:alpha:][:alnum:]匹配次数*: 匹配其前面的字符任意次   a,b,ab,lab,act,adb,amnb,   a*b   // b,ab,lab   a.*b  //ab aab,act,adb,amnb   .* :任意长度的任意字符\? :匹配其前面的字符1次或0次   grep --color ‘a\?b’ ceshi.txt //? 要转意以下\{m,n\}  :匹配 前面的字符串至少m次至多n次   \{1,\}   \{0,3\}位置锚定^   行开始$   行结束   grep  ‘b..h$’  /etc/passwd^$ :   空白行   grep ‘^$’  /etc/inittab  |wc -l\< 或 \b:其后面的任意字符必须作为单词首部 出现\> 或\b:其前面的任意字符必须作为单词尾部出现分组\(\)   \(ab\)*   反向引用   \1 : 引用第一个左括号以及与之对应的右括号所包括的所有内容   \2 :   \3 :grep “\(ab\)” test.txtgrep ‘r..t’ /etc/passwd正则表达式   basic REGEXP : 基本   Extended REGEXP:扩展基本正则表达式.:[]:[^]:次数匹配:*:\?: 0次或1次\{m,n\} :至少m次,至多n次.*:锚定:^:$:\<,\b:\>,\b:\(\)\1,\2,\3, …或者|grep:使用基本正则表达式定义的模式来过滤文本的命令;(针对文件每一行)-i-v-o-—color-E :使用扩展正则表达式-A -B-Cgrep —-color -A 2 ‘^core id’ /proc/cpuinfo   //后面2行grep —-color -B 2 ‘^core id’ /proc/cpuinfo //前面2行grep —-color -C 2 ‘^core id’ /proc/cpuinfo /前后2行grep —color -E’C|cat’ test6.txtgrep —color -E ‘(C|c)at’ test6.txtgrep -E =egrepifconfig | egrep —-color ‘\<([0-9]|[1-9][0-9]|[1-9][1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\>’ \.  找一点IPv4:5类  A B C D EB:128-191C:192-223D:
0 0
原创粉丝点击