前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Linux文本处理命令

Linux文本处理命令

作者头像
章鱼喵
发布2018-06-27 15:01:37
1.4K0
发布2018-06-27 15:01:37
举报
文章被收录于专栏:codingcoding

wc

wc [OPTION]... [FILE]...

  • -l: lines
  • -w: words
  • -c: characters
[root@senlong tmp]# wc /etc/passwd
  38   50 1748 /etc/passwd
[root@senlong tmp]# wc -l /etc/passwd # 文件行数
38 /etc/passwd
[root@senlong tmp]# wc -w /etc/passwd # 文件单词数
50 /etc/passwd
[root@senlong tmp]# wc -c /etc/passwd # 文件字节数
1748 /etc/passwd

cut

cut [OPTION]... [FILE]...

  • -d DELIMITER: 指明切割的分隔符
  • -f FILEDS: 指明字段
    • #: 第#个字段
    • #,#[,#]:离散的多个字段,例如1,3,6
    • #-#:连续的多个字段, 例如1-6
    • 混合使用:1-3,7
  • --output-delimiter=STRING 指明输出的分隔符
[root@senlong tmp]# cut -d: -f1 /etc/passwd # 用:切割文件,取第1个字段
[root@senlong tmp]# cut -d: -f1,7 /etc/passwd # 用:切割文件,取第1和第7个字段
[root@senlong tmp]# cut -d: -f1-3,7 --output-delimiter=' ' /etc/passwd  # 用:切割文件,取第1至第3, 第7个字段,并用指定输出的分隔符

sort

sort [OPTION]... [FILE]...

  • -f: 忽略字符大小写
  • -r: 逆序
  • -t DELIMITER: 字段分隔符
  • -k #:以指定字段为标准排序
  • -n: 以数值大小进行排序
  • -u: uniq,排序后去重
[root@senlong tmp]# sort -t: -k3 -n /etc/passwd # 以:为字段分隔符, 取第3个字段并按数值大小排序
[root@senlong tmp]# sort -t: -k3 -n /etc/passwd | cut -d: -f3 # 排序后再进行切割显示

uniq

uniq [OPTION]... [FILE]...

  • -c: 显示每行重复出现的次数;
  • -d: 仅显示重复过的行;
  • -u: 仅显示不曾重复的行;

Note: 连续且完全相同方为重复

uniq 与 sort -u 的区别:uniq能显示每行重复的次数

[root@senlong tmp]# history | cut -d' ' -f5 | sort | uniq -c
[root@senlong tmp]# history | cut -d' ' -f5 | sort | uniq -d
[root@senlong tmp]# history | cut -d' ' -f5 | sort | uniq -u

练习

  • 以冒号分隔,取出/etc/passwd文件的第6至第10行,并将这些信息按第3个字段的数值大小进行排序;最后仅显示的各自的第1个字段
[root@senlong tmp]# cut -d: -f6-10 /etc/passwd | sort -t: -k3 -n| cut -d: -f1
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017.02.05 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • wc
  • cut
  • sort
  • uniq
  • 练习
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档