Linux-xargs-seq-tr-uniq-sort

Linux-xargs-seq-tr-uniq-sort


一、xrags

| 将前面一个命令的正确的输出送给后面的命令作为输入
xargs 将前面命令的输出送给后面的命令作为参数使用
bash 复制代码
[root@hz shell]# which mkdir
/usr/bin/mkdir
[root@hz shell]# which mkdir| ls -l
总用量 76
drwxr-xr-x. 2 root root   6  5月 16 16:03 20250516160231
-rw-r--r--. 1 root root 265  5月 27 18:05 back_log.sh
-rw-r--r--. 1 root root 399  5月 27 18:35 backup_log.sh
-rw-r--r--. 1 root root 526  5月 18 14:05 create_file.sh
-rw-r--r--. 1 root root 502  5月 18 17:10 create_user.sh
...

[root@hz shell]# which mkdir|xargs ls -l
-rwxr-xr-x. 1 root root 69872  4月 21  2024 /usr/bin/mkdir

二、seq

seq - print a sequence of numbers

用于生成连续或指定步长的数字序列,常用于循环、测试或生成固定格式的输入

生成1-5的数字

root@hz shell\]# seq 5 1 2 3 4 5

步长值默认为+1

root@hz shell\]# seq 5 10 5 6 7 8 9 10 \[root@hz shell\]# seq 10 -1 5 10 9 8 7 6 5

等长

root@hz shell\]# seq -w 5 10 05 06 07 08 09 10


三、tr

tr - translate or delete characters

  1. 转换字符

    字母是分别转换的

    root@hz shell\]# echo "abcdaaabbccdd"\|`tr abc 123` 123d1112233dd

    将/etc/passwd文件里的小写字母转换为大写

    root@hz shell\]#cat /etc/passwd\|`tr "[a-z]" "[A-Z]"`

    替换不能为空

    root@hz shell\]#cat /etc/passwd\|tr "\[0-9\]" "" tr: 当不截断设置1 时,字符串2 不能为空

    删除所有的数字

    root@hz shell\]#cat /etc/passwd\|`tr -d "[0-9]" `

    -d, --delete

    delete characters in SET1, do not translate

    去除%

    root@hz shell\]# df -Th\|grep "/boot" /dev/sda1 xfs 960M 225M 736M 24% /boot \[root@hz shell\]# df -Th\|grep "/boot"\|awk '{print $6}' 24% \[root@hz shell\]# df -Th\|grep "/boot"\|awk '{print $6}'\|`tr -d "%"` 24

    去重,重复的字符串,只显示一个

    root@hz shell\]# echo "111111111112333333333333"\|tr -s "13" 123


四、uniq

uniq - report or omit repeated lines uniqe 唯一

用于筛选或统计相邻的重复行(需先排序,否则非相邻重复行无法识别)

-c 统计重复的次数 count
-u 统计不重复的行

bash 复制代码
[root@hz shell]# cat test.txt|uniq -c
      6 sanchuang1
      1 sanchuang2
      1 
      3 sanchuang1
      1 sanchuang3
      2 sanchuang1

[root@hz shell]# cat test.txt|sort		# 排序

sanchuang1
sanchuang1
sanchuang1
sanchuang1
sanchuang1
sanchuang1
sanchuang1
sanchuang1
sanchuang1
sanchuang1
sanchuang1
sanchuang2
sanchuang3
[root@hz shell]# cat test.txt|sort|uniq -c
      1 
     11 sanchuang1
      1 sanchuang2
      1 sanchuang3

[root@hz shell]# cat test.txt|uniq -u
sanchuang2

sanchuang3
[root@hz shell]# cat test.txt|sort|uniq -u

sanchuang2
sanchuang3

五、sort

sort 默认情况下,排序的时候是根据字符串的ASCII码值进行比较,一个字符一个字符的比较,默认是升序

-n 把数字当做一个整体的数值来比较
-r 降序
-k 指定哪一列为排序键

bash 复制代码
[root@hz shell]# cat grade.txt 
id	name	sex	chinese	English	math
1	cali	m	80		70		60
2	rose	f	90		98		97
3	tom	    f	70		60		60
4	jack	m	99		99		68
# 按math排序
[root@hz shell]# cat grade.txt |sort -k 5
3	tom	    f	70		60		60
1	cali	m	80		70		60
2	rose	f	90		98		97
4	jack	m	99		99		68
id	name	sex	chinese	English	math
# 整体数值比较
[root@hz shell]# cat grade.txt |sort -k 5 -n
id	name	sex	chinese	English	math
3	tom	    f	70		60		60
1	cali	m	80		70		60
2	rose	f	90		98		97
4	jack	m	99		99		68
# 降序
[root@hz shell]# cat grade.txt |sort -k 5 -nr
4	jack	m	99		99		68
2	rose	f	90		98		97
1	cali	m	80		70		60
3	tom	    f	70		60		60
id	name	sex	chinese	English	math
[root@hz shell]# cat grade.txt |sort -k 5 -nr|head -1
4	jack	m	99		99		68

sort -t 指定字段分割符(默认是空白)

对/etc/passwd文件进行排序,基于uid的大小进行降序排列

cat /etc/passwd|sort -k 3 -t ":" -nr

查看访问量最大的2个地址

awk '{print $1}' access_log|sort|uniq -c|sort -nr|head -2

对/etc/passwd文件进行排序,基于gid(第4列)的大小进行升序排列,取gid最小的前6行

cat /etc/passwd|sort -k 4 -t ":" -n |head -6


总结

xargs、seq、uniq、tr、sort是一组常用的文本处理和批处理工具,它们常通过管道组合使用,完成复杂的文本处理或任务自动化

相关推荐
xjxijd3 分钟前
工业元宇宙 IDC 支撑:数字孪生算法 + 边缘服务器,生产调度响应速度提 3 倍
运维·服务器·算法
xlp666hub7 分钟前
从零手写一个 printf 函数:变参宏与默认参数提升
linux
dblens 数据库管理和开发工具10 分钟前
DBLens:让 SQL 查询更智能、更高效的数据库利器
服务器·数据库·sql·数据库连接工具·dblens
程序员zgh13 分钟前
代码重构 —— 读后感
运维·c语言·开发语言·c++·重构
迅为电子19 分钟前
迅为iTOP-Hi3516开发板linux驱动开发资料全面上线,构建从入门到精通的完整学习路径!
linux·驱动开发·学习
代码游侠26 分钟前
应用——Linux进程通信与信号处理
linux·运维·服务器·笔记·学习·信号处理
HalvmånEver29 分钟前
Linux:Ext系列⽂件系统(二)
linux·运维·服务器
石像鬼₧魂石30 分钟前
内网渗透靶场 攻击 & 排错命令分类速查表
linux·windows·学习·ubuntu
信仰JR31 分钟前
Linux系统安装Maven私服Nexus3.X
linux·运维·maven
scan72435 分钟前
python mcp 打印出参数
linux·服务器·数据库