Linux学习之shell5

本文详细介绍了Linux中的四个常用文本处理命令:cut(列提取)、sort(排序)、uniq(取消重复行)和wc(统计行数、单词数和字符数)。通过实例展示了如何使用这些命令处理文本文件,包括列选择、字符范围提取、排序规则以及去重操作。
摘要由CSDN通过智能技术生成

1.cut 列提取命令

语法
cut [选项] 文件名

选项:
-f 列号: 提取第几列
-b 以字节为单位进行分割。这些字节位置将忽略多字节字符边界,除非也指定了 -n 标志。
-d 分隔符: 按照指定分隔符分割列
-n 取消分割多字节字符
-c 字符范围: 不依赖分隔符来区分列,而是通过字符范围(行首为0)来进行字段提取。
“n-”表示从第n个字符到行尾;“n-m”从第n个字符到第m个字符;“一m”表示从第1个字符到第m个字符。
--complement	补足被选择的字节、字符或字段
--out-delimiter	指定输出内容是的字段分割符

示例:

新建txt文件

[root@VM-12-14-centos ~]# touch text16.txt
[root@VM-12-14-centos ~]# chmod 777 text16.txt
[root@VM-12-14-centos ~]# vim text16.txt

id	name	age	    gender
1	zs	    22		M
2	ls		28		M
3	ww		31		F

-f

#查询第二行
[root@VM-12-14-centos ~]# cut -f 2 test16.txt
name
zs
ls
ww
#查询一二行,中间用逗号隔开
[root@VM-12-14-centos ~]# cut -f 1,2 test16.txt
d       name
1       zs
2       ls
3       ww
#相同行数只会查询一个
[root@VM-12-14-centos ~]# cut -f 2,2 test16.txt
name
zs
ls
ww
#行数由小到大输出
[root@VM-12-14-centos ~]# cut -f 2,1 test16.txt
d       name
1       zs
2       ls
3       ww

-b

提取每行的第三个元素
[root@VM-12-14-centos ~]# cut -b 3 test16.txt
n
z
l
w
提取每行的第一,第三元素
[root@VM-12-14-centos ~]# cut -b 1,3 test16.txt
dn
1z
2l
3w
提取每行的一到五元素
[root@VM-12-14-centos ~]# cut -b 1-5 test16.txt
d       nam
1       zs
2       ls
3       ww

截取每一行的第3位之前或之后的字符串

第3位之前(含第3位)
cut -b -3
[root@VM-12-14-centos ~]# cut -b -3 test16.txt
d       n
1       z
2       l
3       w

第3位之后(含第3位)
cut -b 3-
[root@VM-12-14-centos ~]# cut -b 3- test16.txt
name    age    gender
zs      22     M
ls      28     M
ww      31     F

-d

142;145;65

[root@VM-12-14-centos ~]# cut -d ";" -f 1 text17.txt
142
[root@VM-12-14-centos ~]# cut -d ":" -f 1 text17.txt
14;458;41

-d必须和-f配合使用,不然会报错
[root@VM-12-14-centos ~]# cut -d ";"  text17.txt
cut: you must specify a list of bytes, characters, or fields
Try 'cut --help' for more information.

-n

-n会把多字符当成一个字符,而-d会拆分多字符为单字符
[root@VM-12-14-centos ~]# cut -b 1 text18.txt
�
�
�

[root@VM-12-14-centos ~]# cut -b 1,2 text18.txt
�
�
�

[root@VM-12-14-centos ~]# cut -nb 1,2 text18.txt
消失
的
时间

[root@VM-12-14-centos ~]# cut -nb 1 text18.txt
消
的
时

cut在处理多空格间隔的域时,比较麻烦,它擅长处理『以一个字符间隔』的文本内容。

2.sort排序命令

sort工具用于排序;它将文件的每一行作为一个单位,从首字符向后,依次按ASCII码值进行比较,最后将他们按升序输出

语法
sort [选项] 文件名

-u 去除重复行
-r 降序排列,默认是升序
-o 将排序结果输出到文件中,类似重定向符号>
-n 以数字排序,默认是按字符排序
-t 分隔符
-k 第N列
-b 忽略前导空格。
-R 随机排序,每次运行的结果均不同

示例

sort -u 文件名

[root@VM-12-14-centos ~]# sort -u text19.txt 
adsad
aname
cname
dname
hname

sort -r 文件名

[root@VM-12-14-centos ~]# sort -r text19.txt
hname
dname
cname
cname
aname
adsad

sort -u 要排序的文件名 -o 接收排序的文件名
[root@VM-12-14-centos ~]# -u text19.txt -o text20.txt
text20.txt
adsad
aname
cname
dname
hname

vim text21.txt

7551
152
5697
1111
954
258

sort -n 文件名

[root@VM-12-14-centos ~]# sort text21.txt
1111
152
258
5697
7551
954
[root@VM-12-14-centos ~]# sort -n text21.txt
152
258
954
1111
5697
7551

sort -k -t 文件名

[root@VM-12-14-centos ~]# sort -n -k 2 -t : text22.txt
dwag:wcfa
gsdg:awar
gwwaf:aad
[root@VM-12-14-centos ~]# sort -k 2 -t : text22.txt
gwwaf:aad
gsdg:awar
dwag:wcfa

sort -R text21.txt

[root@VM-12-14-centos ~]# sort -R text21.txt
258
152
1111
7551
954
5697
[root@VM-12-14-centos ~]# sort -R text21.txt
152
954
7551
5697
258
1111

3.uniq 取消重复行

语法:

uniq [选项] 文件名

选项:
	-i:忽略大小写

示例

vim text23.txt

Afgw
afgw
lgew
lgew
Lgew

[root@VM-12-14-centos ~]# uniq text23.txt
Afgw
afgw
lgew
Lgew
[root@VM-12-14-centos ~]# uniq -i text23.txt
Afgw
lgew

4.wc 统计命令

语法:
wc [选项] 文件名
选项:
	-l:只统计行数
	-w:只统计单词数
	-m:只统计字符数

#行数单词数,字符数
[root@VM-12-14-centos ~]# wc text23.txt
 5  5 25 text23.txt
[root@VM-12-14-centos ~]# wc -l text23.txt
5 text23.txt
[root@VM-12-14-centos ~]# wc -w text23.txt
5 text23.txt
[root@VM-12-14-centos ~]# wc -m text23.txt
25 text23.txt

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值