课时3:处理信息命令

1.1.2 处理信息

学习目标

这一节,我们从 cut、tr、uniq 、小结 四个方面来学习。
在这里插入图片描述

cut命令

数据截取

Mandatory arguments to long options are mandatory for short options too.
  -b, --bytes=列表              只选中指定的这些字节
  -c, --characters=列表         只选中指定的这些字符
  -d, --delimiter=分界符        使用指定分界符代替制表符作为区域分界
  -f, --fields=LIST       select only these fields;  also print any line
                            that contains no delimiter character, unless
                            the -s option is specified
  -n                      with -b: don't split multibyte characters
      --complement              补全选中的字节、字符或域
  -s, --only-delimited          不打印没有包含分界符的行
      --output-delimiter=字符串 使用指定的字符串作为输出分界符,默认采用输入
                                的分界符
      --help            显示此帮助信息并退出
      --version         显示版本信息并退出
# cut -d: -f1 1.txt 			以:冒号分割,截取第1列内容
# cut -d: -f1,6,7 1.txt 		以:冒号分割,截取第1,6,7列内容
# cut -c4 1.txt 				截取文件中每行第4个字符
# cut -c1-4 1.txt 				截取文件中每行的1-4个字符
# cut -c5- 1.txt 				从第5个字符开始截取后面所有字符

在这里插入图片描述
在这里插入图片描述

tr命令

字符转换、替换、删除

用法:tr [选项]... SET1 [SET2]
从标准输入中替换、缩减和/或删除字符,并将结果写到标准输出。

  -c, -C, --complement          首先补足SET1
  -d, --delete                  删除匹配SET1 的内容,并不作替换
  -s, --squeeze-repeats 		如果匹配于SET1 的字符在输入序列中存在连续的
                                重复,在替换时会被统一缩为一个字符的长度
  -t, --truncate-set1           先将SET1 的长度截为和SET2 相等
      --help            		显示此帮助信息并退出
      --version         		显示版本信息并退出
      
用法1:把commands命令输出做为tr输入进行处理
commands | tr  'string1'  'string2'

用法2:把文件中的内容输入给tr进行处理
tr  'string1'  'string2' < filename

用法3:把文件中的内容输入给tr进行处理,需要使用到选项
tr options 'string1' < filename
示例1:通过tr把反复出现的内容进行压缩,压缩后再处理。
[root@localhost ~]# ifconfig eth0 | grep -w inet
        inet 10.0.0.12  netmask 255.255.255.0  broadcast 10.0.0.255
[root@localhost ~]# ifconfig eth0 | grep -w inet | tr -s " "
 inet 10.0.0.12 netmask 255.255.255.0 broadcast 10.0.0.255
[root@localhost ~]# ifconfig eth0 | grep -w inet | tr -s " " | cut -d " " -f 3  10.0.0.12
示例2: 文件的演示
[root@localhost ~]# head -n 5 /etc/passwd > test1.txt
[root@localhost ~]# tr '[0-9]' '@' < test1.txt
root:x:@:@:root:/root:/bin/bash
bin:x:@:@:bin:/bin:/sbin/nologin
daemon:x:@:@:daemon:/sbin:/sbin/nologin
adm:x:@:@:adm:/var/adm:/sbin/nologin
lp:x:@:@:lp:/var/spool/lpd:/sbin/nologin

[root@localhost ~]# tr '[a-z]' '[A-Z]' < test1.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
ADM:X:3:4:ADM:/VAR/ADM:/SBIN/NOLOGIN
LP:X:4:7:LP:/VAR/SPOOL/LPD:/SBIN/NOLOGIN

在这里插入图片描述
在这里插入图片描述

uniq命令

连续信息去重

Mandatory arguments to long options are mandatory for short options too.
  -c, --count           统计重复行次数
  -d, --repeated        只显示重复行
  -i, --ignore-case     忽略大小写
  -s, --skip-chars=N    avoid comparing the first N characters
  -u, --unique          only print unique lines
文件内容
[root@localhost ~]# cat uniq.txt
AA
aa
aa
bb
cc
cc
dd

去重演示
[root@localhost ~]# uniq uniq.txt
AA
aa
bb
cc
dd

其他演示
uniq -i uniq.txt			大小写不敏感去重
uniq -ic uniq.txt			大小写不敏感去重后计数
uniq -d uniq.txt			仅显示重复的内容
sort -n num.txt  | uniq		结合sort排序后去重

在这里插入图片描述
在这里插入图片描述

扫描二维码关注公众号,回复: 14562822 查看本文章

小结


猜你喜欢

转载自blog.csdn.net/weixin_48502062/article/details/128368171