设为首页 收藏本站
查看: 1134|回复: 0

[经验分享] Centos7文本处理工具

[复制链接]

尚未签到

发表于 2019-2-15 13:17:55 | 显示全部楼层 |阅读模式
  ## 实验一:查看文件内容和比较两文件
  **目的**
  熟练使用cat、less、head、tail、diff等命令。
  **前提**
  可用的centos7系统,连接网络。
  **命令介绍**
  **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 &quot;\([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}&quot;
  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 &quot;.&quot;|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 &quot;^[/].*/&quot;
  /etc/rc.d/init.d/
  ```
  ------




运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-672462-1-1.html 上篇帖子: CentOS 7服务器上使用Nginx+phpMyAdmin 下篇帖子: centos安装报错Generating: “/run/initramfs/rdsosreport
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表