通配符(Globbing)
通配符与元字符类似,通配符主要用于文件名的匹配,而元字符则主要用在字符串的匹配上;
下面介绍几种常用的通配符:* 表示匹配任意位数的任意字符
? 表示匹配一位任意字符 ^ 表示取反,不包含的意思 [] 表示此区间内的任意一个字符 {} 表示一种集合 / 转义字符,使具有特殊意义的字符失去原有意义 | 表示‘或',匹配一组可选的字符元字符
元字符是用来描述字符的特殊字符。
常用的元字符及意义如下:* 重复前面的字符0次或者多次. 匹配任意字符一次/+ 匹配前面的字符1次或者多次/? 匹配前面的字符0次或者1次/{m/} 匹配其前面的字符m次/{m,n/} 匹配前面的字符至少m次,至多n次^ 匹配字符在行首$ 匹配字符在行尾^$ 匹配空白行。空格、0不算/< 匹配字符在词首/> 匹配字符在词尾/<string/> 精准匹配string/(xy/) xy表示一个分组/1 模式从左侧起,第一个左括号以及与之匹配的右括号之间的模式所匹配的字符
除了以上的常用的元字符,还有一些特殊的元字符:
[:alpha:] 所有大小写字母[:upper:] 所有大写字母 [:lower:] 所有小写字母[:alnum:] 所有字母及数字[:punct:] 所有标点符号[:blank:] 空白键和TAB键[:space:] 任意空白的字元,空格、tab、CR等[:digit:] 任意数字,0-9[:print:] 任何可以被打印出来的字符
grep
grep, egrep, fgrep - print lines matching a pattern【SYNOPSIS】 grep [OPTIONS] PATTERN [FILE...] grep [OPTIONS] [-e PATTERN | -f FILE] [FILE...]【OPTIONS】 --color=auto 对匹配到的内容进行高亮显示处理 -i,--ignore-case Ignore case distinctions in both the PATTERN and the input files. (-i is specified by POSIX.)忽略字符大小写匹配 -v,--invert-match Invert the sense of matching, to select non-matching lines. (-v is specified by POSIX.)显示没有匹配到的行 -o,--only-matching Print only the matched (non-empty) parts of a matching line, with each such part on a separate output line.只显示匹配到的部分 -q,--quiet,--silent静默模式,不列举任何内容 -w,--word-regexp 单词完整匹配所在的行 -d, --directories=ACTION how to handle directories; ACTION is 'read', 'recurse', or 'skip',目录表示方式:只读、递归、跳过 -r,-r, --recursive like --directories=recurse -c,--count print only a count of matching lines per FILE匹配到的文件有多少行 -B,--before-context=NUM print NUM lines of leading context列出匹配到的前NUM行 -A,--after-context=NUM print NUM lines of trailing context列出匹配到的后NUM行 -C,--context=NUM print NUM lines of output context列出匹配到的前后几行
cut
Print selected parts of lines from each FILE to standard output列举每行被选中的部分到标准输出,也就是提取行中的某个字段【SYNOPSIS】cut OPTION... [FILE]...【OPTION】 -b,--bytes=LIST select only these bytes按字节分隔 -c,--characters=LIST select only these characters按字符分隔 -d,--delimiter=DELIM use DELIM instead of TAB for field delimiter 用TAB替换指定的分隔符来分区域 -f,--field=LIST 分区域后,根据区域位数来列出数据 -n with -b: don't split multibyte characters不分隔多字节字符【FOR EXAMPLE】 [root@localhost ~]# cat /etc/passwd|cut -d: -f1 root bin daemon adm lp 提取/etc/passwd文件的第一个字段内容,也就是用户名 [root@localhost ~]# cat /tmp/ah2.txt |cut -nb 1,2,3 平凡的 [root@localhost ~]# cat /tmp/ah2.txt |cut -nb 1不分割字节 平 [root@localhost ~]# cat /tmp/ah2.txt |cut -b 1汉字属于多字节字符 [root@localhost ~]# cat /tmp/ah2.txt |cut -c 1 平
新闻热点
疑难解答