1.文件内容浏览
a.cat查看/etc/passwd文件内容,且输出时带行号
[root@localhost ~]# cat -n /etc/passwd
b.使用more/less查看/etc/passwd内容,且每页显示10行
[root@localhost ~]# more -10 /etc/passwd/
[root@localhost ~]# less -10 /etc/passwd/
c.使用head/tail分别查看文件前5行内容和后5行内容
[root@localhost ~]# head -5 /etc/passwd
[root@localhost ~]# tail -5 /etc/passwd
d.使用grep查看/etc/passwd中和root相关的内容
[root@localhost ~]# grep root /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
2.cut命令使用:
给定文件cut_data.txt且内容为:
No Name Score
1 zhang 20
2 li 80
3 wang 90
4 sun 60
使用默认定界符切割文件内容,且输出切割后的第一个字段
切割文件内容,且输出切割后的第一个字段和第三个字段
按字节切割:输出切割的第一个字节到第10个字节的内容
按字符切割:输出切割后的第一个字符和第5个字符的内容
按指定分界符去切割:内容如下, 输出第一个字段和第三个字段内容
No|Name|Score
1|zhang|20
2|li|80
3|wang|90
4|sun|60
给定文件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
3.uniq命令使用: 新建文件uniq_data.txt,文件内容为
Welcome to Linux
Windows
Windows
Mac
Mac
Linux
使用uniq命令输出去重后的结果
使用uniqmingl只输出重复的行
使用uniq命令输出不重复的行
使用uniq命令统计重复次数
新建文件uniq_data.txt文件内容为:
[root@localhost ~]# cat uniq_data.txt
Welcome to Linux
Windows
Windows
Mac
Mac
Linux
(1):使用uniq命令输出去重后的结果:
[root@localhost ~]# cat uniq_data.txt | uniq
Welcome to Linux
Windows
Mac
Linux
(2):使用uniqming只输出重复的行:
[root@localhost ~]# uniq -d uniq_data.txt
Windows
Mac
(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
4.sort命令:给定文件 num.txt, args.txt
文件内容:num.txt
1
3
5
2
4
文件内容:args.txt
test
args1
args2
args4
args4
args3
对num.txt进行排序,且将结果输出到sorted_num.txt中
对args.txt进行排序,且将结果输出到sorted_args.txt中
对num.txt和args.txt进行排序,且将结果输出到sorted_merge.txt中
对args.txt排序后去重输出
合并sorted_args.txt和sorted_num.txt且输出
给定文件info_txt:按第二列作为key进行排序
No Name Score
1 zhang 20
2 li 80
3 wang 90
4 sun 60
给定文件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
(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
5.将26个小写字母的后13个字母替换成大写字母
将26个小写字母的后13个字母替换成大写字母
[root@localhost ~]# echo abcdefghijklmnopqrstuvwxyz | tr n-z N-Z
abcdefghijklmNOPQRSTUVWXYZ
将hello 123 world 456中的数字替换成空字符
[root@localhost ~]# echo hello 123 world 456 | tr 0-9 "\0"
hello world
将hello 123 world 456中字母和空格替换掉,只保留数字
[root@localhost ~]# echo hello 123 world 456 | tr 'a-z ' '\0'
123456