学习的内容来自于Linux Shell脚本攻略(2)
内容只做了简单的记录,方便自己学习
shell 命令查询地址:http://man.linuxde.net/
Grep
在shell中grep担当的责任是搜索文本,他只针对于文本,而不是其他的内容
1)搜索包含特定模式的文本行
grep pattern filename
或者
grep "pattern" filename

2)也可以从stdin中读取

3)单个grep也可以对多个文件执行

4)使用--color选项可以输出行中着重标记出匹配到的单词

5)配合正则进行匹配 需要-E或者egrep

6)只输出文本中匹配的文字

7)对6进行反转 打印出没有匹配到的内容

8)统计打印的行数,这里就会发现grep搜索的模式是以行来计数的

如果要统计个数的话,可以使用wc -l

先按照列输出匹配到的内容,再统计列数
-o 为统计列数
wc -l根据列数显示总列数
7)打印出列数

8)打印模式匹配所位于的字符和字节偏移

9)搜索多个文件并找出匹配文本位于哪一个文件中:
grep -l linux test1.txt test2.txt
-L 返回一个不匹配文本列表
10)指定目录 . 忽略大小写 -i
- 匹配多种样式 grep -e "pattern1" -e "pattern2"
也可以使用先指定一个匹配样式文件 用-f 匹配 grep -f filename
Cut
如果说grep是横向的切,那么cut就是纵向的切
cut -f 1,2 filename
切取 1 2两列
cut -f 1,2 --complement filename
切取除了1和2列以外的内容。
cut -d "xxx" filename 指定字段的定界符


特殊记法
在cut的使用种,我们可以使用下面几个字段指定为某个范围内的字节或字符
-b 表示字节
-c 表示字符
-f 用于定义字段
记法 N表示数字
N- 从第N个字符开始,字符或字段到行尾
N-M 从第N个字节,字符或字段到第M个(包括第M个在内)字节、字符或字段
-M 第1个字节,字符或字段到第M个(包括M在内的)字节、字符或字段

网友评论