cut/wc/head/tail/grep命令

(1),使用cut命令选定字段
cut 
-d : 
-f 1,5 
-f 3- 

-c 1-4 -c以字符数量为标量
-c-4 
-c4- 
-c1,4 
-c1-4,5
【应用实例】
cut命令是用来剪下文本文件里的数据,文本文件可以是字段类型或是字符类型。

/> cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
... ...
/> cut -d : -f 1,5 /etc/passwd     #-d后面的冒号表示字段之间的分隔符,-f表示取分割后的哪些字段
root:root             #这里取出的是第一个和第五个字段。
bin:bin
daemon:daemon
adm:adm
... ...
/> cut -d: -f 3- /etc/passwd  #从第三个字段开始显示,直到最后一个字段。
0:0:root:/root:/bin/bash
1:1:bin:/bin:/sbin/nologin
2:2:daemon:/sbin:/sbin/nologin
3:4:adm:/var/adm:/sbin/nologin
4:7:lp:/var/spool/lpd:/sbin/nologin
... ...    
这里需要进一步说明的是,使用cut命令还可以剪切以字符数量为标量的部分字符,该功能通过-c选项实现,其不能与-d选项共存。
/> cut -c 1-4 /etc/passwd          #取每行的前1-4个字符。
/> cut -c-4 /etc/passwd            #取每行的前4个字符。 
root
bin:
daem
adm:
... ...
/> cut -c4- /etc/passwd            #取每行的第4个到最后字符。
t:x:0:0:root:/root:/bin/bash
:x:1:1:bin:/bin:/sbin/nologin
mon:x:2:2:daemon:/sbin:/sbin/nologin
:x:3:4:adm:/var/adm:/sbin/nologin
... ...
/> cut -c1,4 /etc/passwd           #取每行的第一个和第四个字符。
rt
b:
dm
a:
... ...
/> cut -c1-4,5 /etc/passwd        #取每行的1-4和第5个字符。
root:
bin:x
daemo
adm:x
  

(2),计算行数、字数以及字符数: 
echo This is a test | wc 
echo This is a test | wc -l 统计行 
echo This is a test | wc -w 统计单词数 
echo This is a test | wc -c 统计字符数 

(3),提取开头或结尾数行
 head -n 5 /etc/passwd 
tail -n 5 /etc/passwd 

(4),grep: 
1. grep退出状态: 
0: 表示成功; 
1: 规则不存在; 
2: 文件不存在; 
echo $? 查看退出状态 

2.grep中应用正则表达式 

grep NW testfile #打印所有包含NW的行。 
grep '^n' testfile #打印出以n开头的行。 

grep '4$' testfile #打印出以4结尾的行 

grep '5\..' testfile #打印出第一个字符是5,后面跟着一个.字符。 

grep '\.5' testfile #打印出所有包含.5的行。 

grep '^[we]' testfile #打印出所有以w或e开头的行。 

grep '[^0-9]' testfile #打印出所有不是以0-9开头的行。 

grep '[A-Z][A-Z] [A-Z]' testfile #打印出所有包含前两个字符是大写字符,后面紧跟一个空格及一个大写字母的行。 
当前的语言环境会影响大小写的输出 

grep '[a-z]\{9\}' testfile #打印所有至少有9个连续小写字符的字符串的行。 

grep '\(3\)\.[0-9].*\1 *\1' testfile 
grep '\<north' testfile #打印所有以north开头的单词的行 

grep '\<north\>' testfile #打印所有包含单词north的行。 

grep '^n\w*' testfile #第一个字符是n,后面是任意字母或者数字 

3. 扩展grep #egrep = grep -E 
主要好处是增加了额外的正则表达式元字符集 

egrep 'NW|EA' testfile #打印所有包含NW或EA的行 

grep 'NW\|EA' testfile #对于标准grep,如果在扩展元字符前面加\,grep会自动启用扩展选项-E。 

egrep '3+' testfile 
grep '3\+' testfile #这2条命令将会打印出相同的结果,即所有包含一个或多个3的行。 

egrep '2\.?[0-9]' testfile 
grep '2\.\?[0-9]' testfile #首先含有2字符,其后紧跟着0个或1个点,后面再是0和9之间的数字。 

egrep '(no)+' testfile grep '\(no\)\+' testfile #打印一个或者多个连续的no的行 

egrep'\w+\W+[ABC]' testfile #首先是字母,紧跟着非字母数字,ABC。

egrep 'w(es)t.*\1' testfile 

grep常用的命令行选项: 
-c 只显示有多少行匹配,而不具体显示匹配的行 
-h 不显示文件名 
-i 在字符串比较的时候忽略大小写 
-l 只显示包含匹配模板的行的文件名清单 
-L 只显示不包含匹配模板的行的文件名清单 
-n 在每一行前面打印该行在文件中的行数 
-v 反向检索,只显示不匹配的行 
-w 只显示完整单词的匹配 
-x 只显示完整行的匹配 
-r/-R 如果文件参数是目录,该选项将递归搜索该目录下的所有子目录和文件 
-C 2 打印匹配行及其上下各两行 
-B 2 打印匹配行及其前两行 
-A 2 打印匹配行及其后两行


grep中应用正则表达式的实例:
/> cat testfile
northwest NW Charles Main 3.0 .98 3 34
western WE Sharon Gray 5.3 .97 5 23
southwest SW Lewis Dalsass 2.7 .8 2 18
southern SO Suan Chin 5.1 .95 4 15
southeast SE Patricia Hemenway 4.0 .7 4 17
eastern EA TB Savage 4.4 .84 5 20
northeast NE AM Main Jr. 5.1 .94 3 13
north NO Margot Weber 4.5 .89 5 9
central CT Ann Stephens 5.7 .94 5 13

注:在执行以上命令时,如果不能得到预期的结果,即grep忽略了大小写,导致这一问题的原因很可能是当前环境的本地化的设置问题。对于以上命令,如果我将当前语言设置为en_US的时候,它会打印出所有的行,当我将其修改为中文环境时,就能得到我现在的输出了。
/> export LANG=zh_CN #设置当前的语言环境为中文。
/> export LANG=en_US #设置当前的语言环境为美国。
/> export LANG=en_Br #设置当前的语言环境为英国。

/> grep '[a-z]\{9\}' testfile #打印所有包含每个字符串至少有9个连续小写字符的字符串的行。

#第一个字符是3,紧跟着一个句点,然后是任意一个数字,然后是任意个任意字符,然后又是一个3,然后是制表符,然后又是一个3,需要说明的是,下面正则中的\1表示\(3\)。
/> grep '\(3\)\.[0-9].*\1 *\1' testfile 
northwest NW Charles Main 3.0 .98 3 34

/> grep '\<north' testfile #打印所有以north开头的单词的行。
northwest NW Charles Main 3.0 .98 3 34
northeast NE AM Main Jr. 5.1 .94 3 13
north NO Margot Weber 4.5 .89 5 9

/> grep '\<north\>' testfile #打印所有包含单词north的行。
north NO Margot Weber 4.5 .89 5 9

/> grep '^n\w*' testfile #第一个字符是n,后面是任意字母或者数字。


/> egrep '(no)+' testfile
/> grep -E '(no)+' testfile
/> grep '\(no\)\+' testfile #3个命令返回相同结果,即打印一个或者多个连续的no的行。

/> grep -E '\w+\W+[ABC]' testfile #首先是一个或者多个字母,紧跟着一个或者多个非字母数字,最后一个是ABC中的一个。

/> egrep '[Ss](h|u)' testfile
/> grep -E '[Ss](h|u)' testfile
/> grep '[Ss]\(h\|u\)' testfile #3个命令返回相同结果,即以S或s开头,紧跟着h或者u的行。

/> egrep 'w(es)t.*\1' testfile #west开头,其中es为\1的值,后面紧跟着任意数量的任意字符,最后还有一个es出现在该行。


/> grep -n '^south' testfile #-n选项在每一个匹配行的前面打印行号。

/> grep -i 'pat' testfile #-i选项关闭了大小写敏感。

/> grep -v 'Suan Chin' testfile #打印所有不包含Suan Chin的行。

/> grep -l 'ss' testfile #-l使得grep只打印匹配的文件名,而不打印匹配的行。
testfile
/> grep -c 'west' testfile #-c使得grep只打印有多少匹配模板的行。
3
/> grep -w 'north' testfile #-w只打印整个单词匹配的行。
 
/> grep -C 2 Patricia testfile #打印匹配行及其上下各两行。

/> grep -B 2 Patricia testfile #打印匹配行及其前两行。

/> grep -A 2 Patricia testfile #打印匹配行及其后两行。
  

  

posted @ 2019-03-20 09:24  山的那一边  阅读(154)  评论(0)    收藏  举报