Centos7文本处理工具

## 实验一:查看文件内容和比较两文件


**目的**


熟练使用cat、less、head、tail、diff等命令。



**前提**


可用的centos7系统,连接网络。

扫描二维码关注公众号,回复: 4648788 查看本文章



**命令介绍**



**1、cat命令:查看文件全部内容**


【例1】查看1.sh文件内容


```

[root@han ~]# cat 1.sh 


this is 111 line


this is 222 line


this is 333 line


this is 444 line


this is 555 line


this is 666 line


this is 777 line


this is 888 line


this is 999 line

```





**2、less命令:分页显示文件内容**


【例2】分页查看/var/log/messages文件,文件最后不退出


```

[root@han ~]# less /var/log/messages

```


退出按q键。



**3、head命令:查看文件首部的内容**


【例3】查看1.sh文件的前3行内容


```

[root@han ~]# head -3 1.sh 


this is 111 line


this is 222 line


this is 333 line

```



**4、tail命令:查看文件尾部的内容**


【例4】查看1.sh文件的后3行内容


```

[root@han ~]# tail -3 1.sh 


this is 777 line


this is 888 line


this is 999 line

```



【例5】监视查看1.sh文件尾部是否有内容增加


```

[root@han ~]# tail -f 1.sh

```


按ctrl+c键退出。



**5、diff命令:比较两文件**


【例6】比较1.sh和2.sh两文件的不同


```

[root@han ~]# diff 1.sh 2.sh


1,8d0


< this is 111 line


< this is 222 line


< this is 333 line


< this is 444 line


< this is 555 line


< this is 666 line


< this is 777 line


< this is 888 line


9a2,9


> this is 888 line


> this is 777 line


> this is 666 line


> this is 555 line


> this is 444 line


> this is 333 line


> this is 222 line


> this is 111 line

```


------


## **实验二:指定文件内容抽取字段、统计、排序**



**目的**


熟练使用cut、sort、uniq、wc等命令应用。



**前提**


可用的centos7系统,连接网络。



**命令介绍**



**1、cut命令:按列抽取文本内容**


【例1】截取/etc/passwd文件第一行,以冒号为分隔符,抽取第7个字段


```

[root@han ~]# head -1 /etc/passwd 


root:x:0:0:root:/root:/bin/bash


[root@han ~]# head -1 /etc/passwd | cut -d: -f7


/bin/bash

```



**2、sort命令:文本排序**


【例2】以1.sh文件一行内容的空格分隔,按第3段从大到小排序


```

[root@han ~]# cat 1.sh 


this is 111 line


this is 222 line


this is 333 line


this is 444 line


this is 555 line


this is 666 line


this is 777 line


this is 888 line


this is 999 line


[root@han ~]# cat 1.sh |sort -k3 -r 


this is 999 line


this is 888 line


this is 777 line


this is 666 line


this is 555 line


this is 444 line


this is 333 line


this is 222 line


this is 111 line

```



**3、wc命令:文本数据统计**


【例3】统计/etc/pass文件有多少行


```

[root@han ~]# cat /etc/passwd | wc -l


50

```



**4、uniq命令:文本去重**


【例4】统计2.sh文件中相同内容的行出现的次数


```

[root@han ~]# cat 2.sh 


this is 111 line


this is 111 line


this is 111 line


this is 111 line


this is 111 line


[root@han ~]# uniq -c 2.sh 


      5 this is 111 line

```


------




## 实验三:grep命令和正则表达式应用


**目的**:


熟练使用grep和正则表达式的应用。



**前提**


可用的centos7系统,连接网络。



**命令介绍**



**1、grep命令:根据指定的匹配模式对文本内容进行搜索**


【例1】查找/etc/passwd文件里包含root字符串的行


```

[root@han ~]# grep root /etc/passwd


root:x:0:0:root:/root:/bin/bash


operator:x:11:0:operator:/root:/sbin/nologin

```



【例2】查找2.sh文件里显示不包含111字符串的行


```

[root@han ~]# cat 2.sh 


this is 111 line


this is 222 line


this is 333 line


this is 444 line


this is 555 line


[root@han ~]# grep -v 111 2.sh 


this is 222 line


this is 333 line


this is 444 line


this is 555 line

```





【例3】显示/etc/passwd文件中以bash结尾的行


```

[root@han ~]# grep  'bash$' /etc/passwd


root:x:0:0:root:/root:/bin/bash


lsj:x:1000:1000:lsj:/home/lsj:/bin/bash


linux:x:1004:1004::/home/linux:/bin/bash


liubei:x:1005:1005::/home/liubei:/bin/bash


zhangfei:x:1006:1006::/home/zhangfei:/bin/bash


guanyu:x:1007:1007::/home/guanyu:/bin/bash

```



【例4】找出“ldd /usr/bin/cat”命令的结果中的文件路径


```

[root@han ~]# ldd /usr/bin/cat | grep -o '/[^[:space:]]\+'


/lib64/libc.so.6


/lib64/ld-linux-x86-64.so.2

```



【例5】找出ifconfig命令结果中所有IPv4地址


```

[root@han ~]# ifconfig ens33|grep -o "\([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}"


172.18.118.155


255.255.0.0


172.18.255.255

```



【例6】将此字符串:welcome to  han linux 中的每个字符去重并排序,重复次数多的排到前面


```

[root@han ~]# echo welcome to  han linux|grep -o "."|sort|uniq -c|sort -nr


      3 e


      3  


      2 u


      2 o


      2 m


      2 l


      1 x


      1 w


      1 t


      1 n


      1 i


      1 g


      1 d


      1 c


      1 a

```



**2、egrep命令:同grep命令,但支持扩展的正则表达式**


【例8】使用egrep取出/etc/rc.d/init.d/functions路径的目录名


```

[root@han ~]# echo /etc/rc.d/init.d/functions |egrep -o "^[/].*/"


/etc/rc.d/init.d/

```


------




猜你喜欢

转载自blog.51cto.com/14127616/2335264