Linux 文本处理三剑客:
- grep :文本过滤器(在大段指定的文件中,按一定的模式 patten)进
行过滤)grep 有三种形式:grep egrep fgrep(grep 家族) - sed:stream editor 文本编辑器(用来操作纯 ASCII 码的文本)在操作
文本的时候是按行进行操作的 所以也叫行编辑器 那说到这大家应
该明白 我们的编辑器分为两类 还有一类叫做全屏编辑器,我们打开
一个编辑器,它会占据整个屏幕,然后提供给我们一个编辑窗口,而
后我们就可以在编辑窗口中进行操作了(vim vi) - awk:三位创始人名字的缩写 文本报告生成器 (能够将给定文本以非
常美观的形式显示出来)
grep :
其实 grep 是一段话的缩写:Global search regular expression and print
out the line(全面搜索研究正则表达式并显示出来)
作用:grep 命令是一种强大的文本搜索工具,根据用户指定的“模
式”对目标文本进行匹配检查,打印匹配到的行。
模式(patten):由正则表达式或者字符及基本文本字符所编写的过
滤条件
分文两类:
- 基本正则表达式(BRE):grep
- 扩展正则表达式 (ERE): egrep grep –E
gawk 能够识别 ERE 模式,但 sed 编辑器不能
记住:sed 编辑器和 gawk 的正则表达式引擎之间是有区别的,gawk
可以使用大多数扩展正则表达式模式符号,并且提供一些额外的 sed
编辑器没有的额外过滤功能,但正因为如此,它通常在处理数据流时
更慢
grep 的格式:
指定选项 而后指定模式 指定选项即可 (表示从这个文件中以这个
模式匹配 符合这个模式的行 都予以显示)这时 grep 最简单的用法
(模式当中 只要不涉及变量的话 单双引号都可以 双引号:弱引用
单引号:强引用)
说起 grep 就不得说一下基本正则表达式,因为 grep 是根据基本正则
基本正则表达式 字符匹配:
匹配单个任意字符(出现元字符 用引号引起来 “”:弱引用(意味
着:双引号中间有变量的话,变量可先被替换) ‘’:强引用(直接
匹配每个字符))
实例
[kiosk@asimov ~]$ grep ‘s…n’ /etc/passwd
[kiosk@asimov ~]$ grep ‘s.n’ /etc/passwd
##匹配任意三个三 个 字 母 后 面 接 一 个 t
[kiosk@asimov ~]$ grep ‘[[:alpha:]][[:alpha:]][[:alpha:]]t’ /etc/passwd
##这样写有点麻烦,直接用正则
[kiosk@asimov ~]$ grep ‘[[:alpha:]]{3}t’ /etc/passwd
简单说下正则:
基本正则表达式匹配字数:用在指定字符的后面,用于指定前面的字
符要出现的次数:
*:匹配前面的字符任意次(仅表示次数 0 次 1 次 多次)
grep ‘x*y’ test
abxy xay 与 globe 的区别 (行的:为什么,因为 y 前面的 x 可以出
现 0 次啊,只要 y 匹配了就行了啊)xxxxxxxxy (全部匹配 默认情况下:
正则表达式工作于贪婪模式)
x 不匹配,因为说 x 可以匹配任意次,但 y 没有匹配任意次啊这一点
一定要注意
.*:.表示任意字符 *任意长度 (这才是任意字符任意长度)
grep ‘a.*y’ (a 和 y 之间可以有任意字符)
\?:(?在 bash shell 中有特殊意义 所以要有转义字符):匹配前面的
字符 0 次或者一次(前面的字符可有可无)
grep ‘x\?y’ test (有的话 只匹配一次 没有也可以没有)
\+:匹配其前面的字符,至少出现一次(一次或者多次)
grep ‘x\+y’ test(n 能多长就多长 但不能没有)
回到之前:匹配三个字母
精确匹配:\{m\}精确匹配字符 m 次:
({}在 bsah shall 有特殊意义 命
令行展开)
grep的一些命令参数:
[kiosk@asimov ~]$ grep --help
Usage: grep [OPTION]... PATTERN [FILE]...
Search for PATTERN in each FILE or standard input.
PATTERN is, by default, a basic regular expression (BRE).
Example: grep -i 'hello world' menu.h main.c
Regexp selection and interpretation:
-E, --extended-regexp PATTERN is an extended regular expression (ERE)
-F, --fixed-strings PATTERN is a set of newline-separated fixed strings
-G, --basic-regexp PATTERN is a basic regular expression (BRE)
-P, --perl-regexp PATTERN is a Perl regular expression
-e, --regexp=PATTERN use PATTERN for matching
-f, --file=FILE obtain PATTERN from FILE
-i, --ignore-case ignore case distinctions
-w, --word-regexp force PATTERN to match only whole words
-x, --line-regexp force PATTERN to match only whole lines
-z, --null-data a data line ends in 0 byte, not newline
Miscellaneous:
-s, --no-messages suppress error messages
-v, --invert-match select non-matching lines
-V, --version display version information and exit
--help display this help text and exit
Output control:
-m, --max-count=NUM stop after NUM matches
-b, --byte-offset print the byte offset with output lines
-n, --line-number print line number with output lines
--line-buffered flush output on every line
-H, --with-filename print the file name for each match
-h, --no-filename suppress the file name prefix on output
--label=LABEL use LABEL as the standard input file name prefix
-o, --only-matching show only the part of a line matching PATTERN
-q, --quiet, --silent suppress all normal output
--binary-files=TYPE assume that binary files are TYPE;
TYPE is 'binary', 'text', or 'without-match'
-a, --text equivalent to --binary-files=text
-I equivalent to --binary-files=without-match
-d, --directories=ACTION how to handle directories;
ACTION is 'read', 'recurse', or 'skip'
-D, --devices=ACTION how to handle devices, FIFOs and sockets;
ACTION is 'read' or 'skip'
-r, --recursive like --directories=recurse
-R, --dereference-recursive
likewise, but follow all symlinks
--include=FILE_PATTERN
search only files that match FILE_PATTERN
--exclude=FILE_PATTERN
skip files and directories matching FILE_PATTERN
--exclude-from=FILE skip files matching any file pattern from FILE
--exclude-dir=PATTERN directories that match PATTERN will be skipped.
-L, --files-without-match print only names of FILEs containing no match
-l, --files-with-matches print only names of FILEs containing matches
-c, --count print only a count of matching lines per FILE
-T, --initial-tab make tabs line up (if needed)
-Z, --null print 0 byte after FILE name
比较常用的命令参数:
[kiosk@asimov ~]$ grep -c ‘[[:alpha:]]{3}t’ /etc/passwd ##统计行数
[kiosk@asimov ~]$ grep -n ‘[[:alpha:]]{3}t’ /etc/passwd ##显示行数
[kiosk@asimov ~]$ grep -on ‘[[:alpha:]]{3}t’ /etc/passwd ##-o只显示字符不显示所在行
##将没有nologin的grep出来
[kiosk@asimov ~]$ grep -v root /etc/passwd | grep -v nologin ##-v反选
grep -r root /mnt 显示root在mnt下的那个文件里
grep ^root passwd root在行首
grep root$ passwd root在行尾
grep -i root passwd 忽略大小写
grep 'root' /etc/passwd | grep -v "(^root)|(root$)" ##root在中间的