前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >常用文本工具cut、wc、sort、uniq、tr

常用文本工具cut、wc、sort、uniq、tr

作者头像
阿dai学长
发布2019-04-03 11:25:26
9870
发布2019-04-03 11:25:26
举报
文章被收录于专栏:阿dai_linux

8.10 shell特殊符号&cut命令

特殊符号

  • “*” 代表零个或多个字符
  • “?” 代表一个字符
  • “#” 注释符号
  • “\” 脱意符号
  • “|” 管道符
  • “$” 该符号与“!”合用“!$”表示上一条命令中的最后一个变量
  • “;” 分隔符,在一行中运行两个及两个以上的命令时使用
  • “~” 用户的家目录(root用户“/root”,普通用户“/home/username”)
  • “&” 如果想把一条命令直接放到后台运行的话,可以在命令行加上这个符号(通常用于运行时间非常长的命令)
  • “[]” 中括号中间为字符组合,代表中间字符中的任意一个。

cut命令

cut命令用来显示行中的指定部分,删除文件中指定字段。cut经常用来显示文件的内容,类似于下的type命令。 说明:该命令有两项功能,其一是用来显示文件的内容,它依次读取由参数file所指明的文件,将它们的内容输出到标准输出上;其二是连接两个或多个文件,如cut fl f2 > f3将把文件fl和几的内容合并起来,然后通过输出重定向符“>”的作用,将它们放入文件f3中。

语法: cut -d ‘分隔符’ [-cf] n [filename] (这里n是正整数) -d:指定分隔符号 -f:指定第几段 -c:后面只有一个数字表示截取第几个字符;后面跟一个数字区域,表示截取从几到几(该选项不和d,f共同使用)

代码语言:javascript
复制
[root@adai003 tmp]# cut -c1 1.txt |head -n2
r
b
[root@adai003 tmp]# cut -c1,3 1.txt |head -n2
ro
bn
[root@adai003 tmp]# cut -f1,3 -d ':' 1.txt |head -n2
root:0
bin:1
[root@adai003 tmp]# cut -f1-3 -d ':' 1.txt |head -n2
root:x:0
bin:x:1

8.11 sort、wc、uniq命令

sort命令

sort命令是在Linux里非常有用,它将文件进行排序,并将排序结果标准输出。sort命令既可以从特定的文件,也可以从stdin中获取输入。

语法: sort [-t 分隔符] [options] [filename] Options: -t:指定分隔符 -n:使用纯数字排序(系统默认所有字母为0) -r:反向排序 -u:=unique 去重复 -kn1,n2:由n1区间排序到n2区间,可以只写-kn1,即对n1字段排序(n1 < n2) sort不加任何选项,则从首字符向后,依次以ASCⅡ码值进行比较,最后将它们按升续输出。

代码语言:javascript
复制
[root@adai003 tmp]# head -n3 1.txt
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
[root@adai003 tmp]# head -n3 1.txt |sort
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
root:x:0:0:root:/root:/bin/bash

wc命令

wc命令用来计算数字。利用wc指令我们可以计算文件的Byte数、字数或是列数。

语法: wc [options] [filename] Options: -l:=line 统计行数 -m:=member 统计字符数 -w:=Word 统计词数

代码语言:javascript
复制
[root@adai003 tmp]# wc -l !$
wc -l 2.txt
2 2.txt
[root@adai003 tmp]# wc -m 2.txt
10 2.txt
[root@adai003 tmp]# cat !$
cat 2.txt
1234
qwer
[root@adai003 tmp]# cat -A 2.txt
1234$
qwer$
[root@adai003 tmp]# wc -w 2.txt
4 2.txt
[root@adai003 tmp]# cat 2.txt
1234 456 789,10
qwer

说明: wc -m会统计文件内所有字符,包括隐藏的换行符“&”;wc -w是以空格作为分隔符进行词组统计的。

uniq命令(unique)

uniq命令用于报告或忽略文件中的重复行,一般与sort命令结合使用(即:去重复)。

语法: uniq [options] [filename] Options: -c:=count 在每列旁边显示该行重复出现的次数

代码语言:javascript
复制
[root@adai003 tmp]# cat !$
cat 2.txt
1234
456 789,10
1234
qwer
456
[root@adai003 tmp]# uniq -c 2.txt
      1 1234
      1 456 789,10
      1 1234
      1 qwer
      1 456
[root@adai003 tmp]# sort 2.txt |uniq -c
      2 1234
      1 456
      1 456 789,10
      1 qwer

说明: 直接使用uniq命令,2.txt内容显示并没有变化,使用sort排序后再用uniq命令,重复行被合并,即:在对文件进行去重之前需要先进行排序!

8.12 tee、tr、split命令

tee命令

tee命令用于将数据重定向到文件,会删除文件内原有内容,与“>”不同的是,tee会把定向的文件内容显示出来。

语法: tee [options] [filename] Options: -a:向文件中重定向时使用追加模式(=“>>”)

代码语言:javascript
复制
[root@adai003 tmp]# cat 3.txt
00000000000
[root@adai003 tmp]# sort 2.txt |uniq -c |tee 3.txt
      2 1234
      1 456
      1 456 789,10
      1 qwer
[root@adai003 tmp]# cat 3.txt
      2 1234
      1 456
      1 456 789,10
      1 qwer
      [root@adai003 tmp]# sort 2.txt |uniq -c |tee -a 3.txt
      2 1234
      1 456
      1 456 789,10
      1 qwer
[root@adai003 tmp]# cat 3.txt
      2 1234
      1 456
      1 456 789,10
      1 qwer
      2 1234
      1 456
      1 456 789,10
      1 qwer

tr命令

tr命令可以对来自标准输入的字符进行替换、压缩和删除,它可以将一个字符变成另一个字符,也可以将一组字符变成另一组字符。

语法: tr [源字符] [目标字符]

代码语言:javascript
复制
[root@adai003 tmp]# echo "adailinux" |tr 'a' 'A'
AdAilinux   替换一个字符
[root@adai003 tmp]# echo "adailinux" |tr '[al]' '[AL]'
AdAiLinux   替换多个字符
[root@adai003 tmp]# echo "adailinux" |tr '[a-z]' '[A-Z]'
ADAILINUX

split命令

split命令可以将一个大文件分割成很多个小文件,有时需要将文件分割成更小的片段,比如为提高可读性,生成日志等。

语法: split [options] [filename] -b:指定每一输出档案的大小,默认单位为 byte,可自定义单位,如 split -b 100M filename -l:指定每一个输出档案的行数多少 eg1: 指定大小

代码语言:javascript
复制
[root@adai003 tmp]# split -b 100 1.txt
[root@adai003 tmp]# ls
xaa
xab
xac
xad
[root@adai003 tmp]# rm -rf x*
[root@adai003 tmp]# split -b 100 1.txt adai.  
可以指定文件前缀!
[root@adai003 tmp]# ls
adai.aa 
adai.ab
adai.ac
adai.ad

eg2: 指定行数

代码语言:javascript
复制
[root@adai003 tmp]# wc -l 1.txt
20 1.txt
[root@adai003 tmp]# split -l 5 1.txt
[root@adai003 tmp]# ls
xaa
xab
xac
xad
[root@adai003 tmp]# wc -l x*
  5 xaa
  5 xab
  5 xac
  5 xad
 20 总用量

8.13 shell特殊符号(下)

命令连接符: “||”、“&&”、“;”

  • command1 ; command2 : 不管command1是否执行成功都会执行command2
  • command1 && command2 : 只有command1执行成功后才会执行command2
  • command1 || command2 : 表示command1执行成功后,command2不执行,否则执行command2

(adsbygoogle = window.adsbygoogle || []).push({});

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017/06/30 ,如有侵权请联系 cloudcommunity@tencent.com 删除

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

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 8.10 shell特殊符号&cut命令
    • 特殊符号
      • cut命令
      • 8.11 sort、wc、uniq命令
        • sort命令
          • wc命令
            • uniq命令(unique)
            • 8.12 tee、tr、split命令
              • tee命令
                • tr命令
                  • split命令
                  • 8.13 shell特殊符号(下)
                  领券
                  问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档