【Linux】文本处理的常用命令

目录

1、awk

2、sort

 3、uniq

 4、tr

5、cut

6、grep

1、awk

AWK 是一种处理文本文件的语言,是一个强大的文本分析工具。

之所以叫 AWK 是因为其取了三位创始人 Alfred Aho,Peter Weinberger, 和 Brian Kernighan 的 Family Name 的首字符。

[root@localhost lianxi]# cat student_info.txt |awk '{print $4}'
grade
89
90
82
93
  • 一种文本截取的命令
  • ‘{ }’是固定的语法

  • print是awk里的命令,用来输出内容

  • $0代表整行

  • $1代表第一个字段

  • $2代表第二个字段

  • awk默认的字段和字段之间的分隔符是空白 --》(空格、tab)

  • , 是输出的时候使用一个空格作为分隔符

2、sort

sort是一个排序命令

  • 默认情况下,根据每一行的首字母进行排序,
  • 根据首字母的ASCII码值进程升序,如果首字母一样,在比较第二个字母,以此类推
  • -n : 按整数进行排序
  • -r : 递减排序
  • -k : 指定哪一列作为排序键
  • -t : 指定字段分割符(默认是空白)
[root@localhost lianxi]# cat student.txt | tr -s " "|sort -k 4 -nr
chen 18 F 99 78 90
feng 20 F 95 89 92
zhao 19 M 88 75 89
cali 36 M 80 67 90
name age sex chinese math English

 3、uniq

  • 删除经过排序后的数据的重复记录
  • 通常和sort连用
  • -c : 统计特定记录出现的次数    cat tt | uniq -c
  • -u : 只显示唯一的行  cat tt | uniq -u
  • -d : 只显示重复的行  cat tt | uniq -d
[root@localhost lianxi]# cat student.txt | awk '{print $3}'|sort|uniq -c|sort -rn |head -2
      2 M
      2 F

 小练习:搭建一个nginx的web服务器,查找那个ip地址访问的次数最多的前3个ip地址

  1. 安装nginx web服务器软件 yum install nginx
  2. 启动nginx服务
    [root@localhost lianxi]# service nginx restart
    Redirecting to /bin/systemctl restart nginx.service
  3.  关闭防火墙服务
    [root@localhost lianxi]# service firewalld stop
    Redirecting to /bin/systemctl stop firewalld.service
    [root@localhost lianxi]# iptables -L   查看防火墙的规则 
  4. 查看自己服务器的ip地址 ip add
  5. 打开浏览器输入自己的ip地址 访问你的web服务器 使用Chrome浏览器访问
    [root@localhost lianxi]# curl http://192.168.136.143     #Linux里访问
    
  6.  修改网站的首页
    [root@localhost lianxi]# cd /usr/share/nginx/html/   #进入nginx存放网页的目录
    [root@localhost html]# ls
    404.html  50x.html  en-US  icons  img  index.html  nginx-logo.png  poweredby.png
    #index.html 是网站看到的第一个页面
    [root@localhost html]# echo "i love linux" > index.html  #替换修改index.html里面的内容
    [root@localhost html]# cat index.html 
    i love linux
     
  7. 刷新刚刚在浏览器打开的网页,会看到我们自己重定向进去的内容
  8. 进入记录nginx日志的目录
    [root@localhost html]# cd /var/log/nginx/
    [root@localhost nginx]# ls
    access.log  error.log
    #access.log 访问日志,记录谁什么时间访问了那个页面,成功还是失败
  9.  统计得出访问量最大的 ip地址
    [root@localhost nginx]# cat access.log |awk '{print $1}'|sort |uniq -c|sort  -rn|head -3
        

 4、tr

tr是字符转换和删除字符的工具  (translate delete)

tr命令需要使用 管道 或者 < 给tr传递参数,不能对问件直接操作

  1. 使用tr转换字符
    [root@localhost lianxi]# echo 123456112233445566 | tr 123 abc
    abc456aabbcc445566
    #将1替换为 a  2--》b 3 --->c

  2.  使用tr压缩字符
  • -d :删除字符串 
  • -s :压缩连续相同的字符串
    [root@localhost lianxi]# echo 12345436345 | tr -d 3 #把3删掉
    12454645
    [root@localhost lianxi]# echo 12345436sdgsadgsdg34tqe345 | tr -d [0-9] #把数字删掉
    sdgsadgsdgtqe
    [root@localhost lianxi]# echo 11222354633344445555| tr -s 345
    112223546345        

5、cut

cut命令

  • 从文本文件或者文本流中提取文本列
  • cut - 选项 提取范围 文本文件

常见选项 

  • -c : 从指定提取范围中提取字符
  • -f : 从指定提取范围中提取字段

提取范围

  • n:第n项
  • n-:第n项到行尾
  • -m:行首到第m项
  • n,m:第n项和第m项(单个的)
  • n-m:第n项到第m项(连续的)

6、grep

文本过滤命令,会根据字符串去匹配内容,默认情况下符合要求(匹配的),会整行的显示出来

  • ^d 表示一行里以d开头的都会显示出来
  • d$ 表示一行里以d结尾的
    [root@loaclhost lianxi]# ll
    总用量 12
    drwxr-xr-x. 4 root root 32 12月 16 21:34 china
    -rw-r--r--. 1 root root 31 12月 22 20:00 datingzi
    -rw-r--r--. 1 root root 21 12月 22 19:32 flshhs
    -rw-r--r--. 1 root root 22 12月 22 19:59 touch
    -rw-r--r--. 1 root root  0 12月 19 10:10 xz.txt
    
    [root@loaclhost lianxi]# ll|grep "^d"
    drwxr-xr-x. 4 root root 32 12月 16 21:34 china
    [root@loaclhost lianxi]# ll|grep "^d"|wc
          1       9      48
    [root@loaclhost lianxi]# ll|grep "^-"|wc
          4      36     198

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值