Linux cat more grep head tail cut uniq sort tr命令详解

cat命令

1 cat在查看文件内容时同时输出行号应该怎么做?

如下:查看/etc/passwd/下的内容并且输出行号
注意:/etc/passwd/ 这个文件下不是关于密码的配置文件,而是关乎于用户的配置文件。
命令:

[root@localhost Desktop]# cat -n /etc/passwd

2 more 命令

例:还是查看/etc/passwd目录下的内容:
more命令

[root@localhost Desktop]# more -p10 /etc/passwd

3 grep命令

例: 查看/etc/passwd 中和root相关的内容:
命令:

[root@localhost Desktop]# grep -G "root" /etc/passwd

4 head , tail 命令

1 head 查看内容

例:查看 /etc/passwd 内容的前5行内容:
命令:

[root@localhost Desktop]# head -n 5 /etc/passwd

默认是输出10行

2 tail 查看内容

例:查看 /etc/passwd 内容的前5行内容:
命令:

[root@localhost Desktop]# tail -n 5 /etc/passwd

默认输出也是10行。

cut命令

首先创建一个文件:cut_data.txt ,且内容为:

No	Name	Score
1		zhang	20
2		li			80
2      wang    90
4       sun     60

1 要求剪切后输出第一个字段

命令:

[root@localhost Desktop]# cut -b 1-2 cut_data

结果如下:

在这里插入图片描述
按字节切割,输出切割后的第一个字节到第10个字节的内容:

[root@localhost Desktop]# cut -b 1-10 cut_data

按字符切割,输出切割后的第一个字符到第5个字符的内容:

[root@localhost Desktop]# cut -c 1-5 cut_data 

将上面文件内容改为:

No|Name|Score
1|zhang|20
2|li|80
2|wang|90
4|sun|60

2 输出第一字段和第三字段内容

说明:因为这里是以 | 作为分隔的所以在用分隔条件时需要加上转义符 ** ,所以是 -d| 。整个命令解释是:将cut_data整个文件的内容作为标准输入到cut语句中,以 | 为分界符(可知分成了3个域),剪切出第1和3域。
命令和输出如下:

[root@localhost Desktop]# cat cut_data | cut -d\| -f 1,3
No|Score
1|20
2|80
2|90
4|60

uniq命令

新建文件uniq_data.txt 并且内容写:

Welcome to Linux
Windows
Windows
Mac
Mac
Linux

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

命令及输出结果:

[root@localhost Desktop]# uniq uniq_data.txt 
Welcome to Linux
Windows
Mac
Linux

注意:默认是去重

2 使用uniq命令只输出重复的行

命令及输出结果:

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

3 使用uniq命令统计重复的次数

命令及结果:

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

注意数字代表重复个数

sort命令

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

 1
3
5
2
4

注意第一行有一个空格。

args.txt内容:

args1
args2
args4
args4
args3

1 对num.txt排序,并且将结果输出到sorted_num.txt中

首先创建文件写入内容不用说了。
命令及结果:

[root@localhost Desktop]# sort -n num.txt > sorted_num.txt
 1
2
3
4
5

注意: -b 参数是不管开头的空白,但这里不加这个参数好像也行。

2 对args.txt排序,并且将结果输出到sorted_args.txt中

命令及显示结果:

[root@localhost Desktop]# cat sorted_args.txt 
[root@localhost Desktop]# cat sorted_args.txt 
args1
args2
args3
args4
args4

3 对args.txt和num.txt排序,并将结果输出到sorted_merge.txt中

命令及结果:

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

4 对args.txt排序后去重输出合并sorted_args.txt和sorted_num.txt且输出给定文件info.txt按第二列作为key进行排序

命令及结果:

[root@localhost Desktop]# sort args.txt | uniq | cat sorted_args.txt sorted_num.txt > info.txt | sort -k 2

注意:这个命令本人不确定对不对,应该与描述的命令一样

tr命令

1 将后14个英文字母变成大写

如下命令及结果:

[root@localhost Desktop]# tr m-z M-Z
abcdefghijklmnopqrstuvwxyz
abcdefghijklMNOPQRSTUVWXYZ

2 将文件中数字变为空字符

aaa.txt中写入:

hello 123 world 456

命令及结果:

[root@localhost Desktop]# cat aaa.txt | tr 0-9 " "-" "
hello     world 

注意:" “-” " 。 ""号中间有个空格

结束

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员Fy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值