@[TOC](第七章 文本处理工具)
实验⼀:查看文件内容和比较两文件
目的
熟练使用cat、less、head、tail、diff等命令。
前提
linux系统,centos6、centos7或ubuntu,连接网络。
命令介绍
1、cat命令:查看文件全部内容
【例1】查看1.sh⽂件内容
[root@Magedu ~]# 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@Magedu ~]# less /var/log/messages
退出按q键。
3、head命令:查看文件首部的内容
【例3】查看1.sh⽂件的前3⾏内容
[root@Magedu ~]# head -3 1.sh
this is 111 line
this is 222 line
this is 333 line
4、tail命令:查看文件尾部的内容
【例4】查看1.sh⽂件的后3⾏内容
[root@Magedu ~]# tail -3 1.sh
this is 777 line
this is 888 line
this is 999 line
【例5】监视查看1.sh⽂件尾部是否有内容增加
[root@Magedu ~]# tail -f 1.sh
按ctrl+c键退出。
5、diff命令:比较两文件
【例6】⽐较1.sh和2.sh两⽂件的不同
[root@Magedu ~]# 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等命令应用。
前提
linux系统,centos6、centos7或ubuntu,连接网络。
命令介绍
1、cut命令:按列抽取⽂本内容
【例1】截取/etc/passwd⽂件第⼀⾏,以冒号为分隔符,抽取第7个字段
[root@Magedu ~]# head -1 /etc/passwd
root:x:0:0:root:/root:/bin/bash
[root@Magedu ~]# head -1 /etc/passwd | cut -d: -f7
/bin/bash
2、sort命令:⽂本排序
【例2】以1.sh⽂件⼀⾏内容的空格分隔,按第3段从⼤到⼩排序
[root@Magedu ~]# 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@Magedu ~]# 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@Magedu ~]# cat /etc/passwd | wc -l
50
4、uniq命令:⽂本去重
【例4】统计2.sh⽂件中相同内容的⾏出现的次数
[root@Magedu ~]# cat 2.sh
this is 111 line
this is 111 line
this is 111 line
this is 111 line
this is 111 line
[root@Magedu ~]# uniq -c 2.sh
5 this is 111 line
实验三:grep命令和正则表达式应用
目的
熟练使用grep和正则表达式的应用。
前提
linux系统,centos6、centos7或ubuntu,连接网络。
命令介绍
1、grep命令:根据指定的匹配模式对文本内容进行搜索
【例1】查找/etc/passwd⽂件⾥包含root字符串的⾏
[root@Magedu ~]# 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@Magedu ~]# cat 2.sh
this is 111 line
this is 222 line
this is 333 line
this is 444 line
this is 555 line
[root@Magedu ~]# 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@Magedu ~]# 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@Magedu ~]# ldd /usr/bin/cat | grep -o '/[^[:space:]]\+'
/lib64/libc.so.6
/lib64/ld-linux-x86-64.so.2
【例5】找出ifconfig命令结果中所有IPv4地址
[root@Magedu ~]# 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 magedu linux 中的每个字符去重并排序,重复次数多的排到前⾯
[root@Magedu ~]# echo welcome to magedu 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@Magedu ~]# echo /etc/rc.d/init.d/functions |egrep -o "^[/].*/"
/etc/rc.d/init.d/