Linux基础---第四天作业--查找,切割,排序

本文详细介绍了Linux中的几个基础命令用法,包括查找命令的whereis和which,find命令的各种查找场景,cut命令的字段切割,uniq命令去重与统计,sort命令的排序操作,以及tr命令的字符转换和删除,还有WC命令的使用。总结了uniq与sort -u的区别,提供丰富的实例解析。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.查找命令:

        (1)使用whereis查找whereis命令:

[root@localhost ~]# whereis whereis
whereis: /usr/bin/whereis /usr/share/man/man1/whereis.1.gz
[root@localhost ~]#

        (2)使用which查找whereis命令:

[root@localhost ~]# which whereis
/usr/bin/whereis
[root@localhost ~]#

2.find命令使用:

        (1)使用find命令在当前路径下查找所有的普通文件:

[root@localhost ~]# find . -type f
./.bash_logout
./.bashrc
./.cshrc
./.tcshrc
./anaconda-ks.cfg
./.tt.swp
./pipe_data.txt

        (2)使用find命令查找当前路径下的file1.txt,file2.txt,file3.txt:

[root@localhost ~]# 
[root@localhost ~]# find . -name "file*"
./test/file1.txt
./test/file2.txt
./test/file3.txt
[root@localhost ~]# 

        (3)使用find命令查找文件所有者为root的普通文件:

[root@localhost ~]# find . -user root | find . -type f
./.bash_logout
./.bashrc
./.cshrc
./.tcshrc
./anaconda-ks.cfg
./.tt.swp
./pipe_data.txt
./.viminfo

        (4)使用find命令查找修改时间在1天以内的普通文件:

[root@localhost ~]# find . -mtime -1
.
./pipe_data.txt
./.viminfo
./sorted_num.txt
./char.txt
./word_count.txt
./args.txt

3.cut命令使用:

        给定文件cut_data.txt且内容为:

[root@localhost ~]# cat cut_data.txt 
No	Name	Score
1	zahng	20
2	li	    80
3	wang	90
4	sun    	60

(1):使用默认定界符切割文件内容,且输出切割后的第一个字段:

[root@localhost ~]# cut -f1 cut_data.txt 
No
1
2
3
4

(2):切割文件内容,且输出切割后的第一个字段和第三个字段:

[root@localhost ~]# cut -f1,3 cut_data.txt 
No	Score
1	20
2	80
3	90
4	60

(3):按字符切割,输出切割后的第一到十个字节的内容:

[root@localhost ~]# cut -b1-10 cut_data.txt
No	Name	Sc
1	zahng	20
2	li	    80
3	wang	90
4	sun	    60

(4):按字符切割,输出切割后的第一个字符到第五个字符的内容:

[root@localhost ~]# cut -c1-5 cut_data.txt
No	Na
1	zah
2	li	
3	wan
4	sun

(5):按指定分界符去切割:内容如下,输出第一个字段和第三个字段内容:

No|Name|Score
1|zhang|20
2|li|80
3|wang|90
4|sun|60
[root@localhost ~]# cut -d"|" -f1,3 cut_data.txt 
No|Score
1|20
2|80
3|90
4|60

4.uniq命令使用:

        新建文件uniq_data.txt文件内容为:

[root@localhost ~]# cat uniq_data.txt 
Welcome to Linux
Windows
Windows
Mac
Mac
Linux
[root@localhost ~]#

(1):使用uniq命令输出去重后的结果:

[root@localhost ~]# cat uniq_data.txt | uniq
Welcome to Linux
Windows
Mac
Linux
[root@localhost ~]#

(2):使用uniqming只输出重复的行:

[root@localhost ~]# uniq -d uniq_data.txt 
Windows
Mac
[root@localhost ~]#

(3):使用uniq命令输出不重复的行:

[root@localhost ~]# uniq -u uniq_data.txt 
Welcome to Linux
Linux

(4):使用uniq命令统计重复次数:

[root@localhost ~]# uniq -c uniq_data.txt 
      1 Welcome to Linux
      2 Windows
      2 Mac
      1 Linux

5.sort命令:

        给定文件num.txt,args.txt,文件内容:

[root@localhost ~]# cat num.txt 
1
3
5
2
4
[root@localhost ~]# cat args.txt 
test
args1
args2
args4
args4
args3

(1):对num.txt进行排序,且将结果输出到sorted_num.txt中:

[root@localhost ~]# sort num.txt > sorted_num.txt
[root@localhost ~]# cat sorted_num.txt 
1
2
3
4
5
[root@localhost ~]#

(2):对args.txt进行排序,且将结果输出到sorted_args.txt中:

[root@localhost ~]# sort args.txt > sorted_args.txt
[root@localhost ~]# cat sorted_args.txt 
args1
args2
args3
args4
args4
test

(3):对 num.txt 和 args.txt 进行排序,且将结果输出到sorted_merge中:

[root@localhost ~]# sort num.txt args.txt > sorted_merge.txt
[root@localhost ~]# cat sorted_merge.txt 
1
2
3
4
5
args1
args2
args3
args4
args4
test

(4):对args.txt排序后去重输出:

[root@localhost ~]# sort args.txt | uniq
args1
args2
args3
args4
test

(5):合并sorted_args.txt和sorted_num.txt且输出:

[root@localhost ~]# cat sorted_args.txt sorted_num.txt 
args1
args2
args3
args4
args4
test
1
2
3
4
5

(6):给定文件info_txt:按第二列作为key进行排序:

[root@localhost ~]# sort -b -k2 info_txt 
2	li	    80
No	Name	Score
4	sun	    60
3	wang	90
1	zhang	20

总结比较 uniq 和 sort -u 的区别:

        sort -u 能够去除不连续的行,uniq 只能去除连续的重复行;举例如下:

[root@localhost ~]# sort -u tt.txt 
hello
world
[root@localhost ~]# cat tt.txt | uniq
hello
world
hello
world

6.tr转换和删除:

        (1)将26个小写字母的后13个字母替换成大写字母

[root@localhost ~]# echo abcdefghijklmnopqrstuvwxyz | tr n-z N-Z
abcdefghijklmNOPQRSTUVWXYZ
[root@localhost ~]#

        (2) 将hello 123 world 456中的数字替换成空字符(提示使用通配符)

[root@localhost ~]# echo hello 123 world 456 | tr 0-9 "\0"
hello  world 
[root@localhost ~]#

        (3)将hello 123 world 456 中字母和空格替换掉,只保留数字(提示使用通配符)

[root@localhost ~]# echo hello 123 world 456 | tr 'a-z ' '\0'
123456
[root@localhost ~]#

7.WC命令使用:

        给定文件:word_count.txt里面填充10行内容:

[root@localhost ~]# cat word_count.txt 
asdasfa
sada......
afasdasd
asd
asf
a
sf
adad
sfafaf
qwe123534
[root@localhost ~]# wc -c word_count.txt 
63 word_count.txt
[root@localhost ~]# wc -w word_count.txt 
10 word_count.txt
[root@localhost ~]# wc -l word_count.txt 
10 word_count.txt
[root@localhost ~]#

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值