当前位置: 首页 > news >正文

Linux-xargs-seq-tr-uniq-sort

Linux-xargs-seq-tr-uniq-sort

  • 一、xrags
  • 二、seq
  • 三、tr
  • 四、uniq
  • 五、sort
  • 总结


一、xrags

|将前面一个命令的正确的输出送给后面的命令作为输入
xargs将前面命令的输出送给后面的命令作为参数使用
[root@hz shell]# which mkdir
/usr/bin/mkdir
[root@hz shell]# which mkdir| ls -l
总用量 76
drwxr-xr-x. 2 root root   6  516 16:03 20250516160231
-rw-r--r--. 1 root root 265  527 18:05 back_log.sh
-rw-r--r--. 1 root root 399  527 18:35 backup_log.sh
-rw-r--r--. 1 root root 526  518 14:05 create_file.sh
-rw-r--r--. 1 root root 502  518 17:10 create_user.sh
...[root@hz shell]# which mkdir|xargs ls -l
-rwxr-xr-x. 1 root root 69872  421  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]"

  2. 删除字符

    -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 统计不重复的行

[root@hz shell]# cat test.txt|uniq -c6 sanchuang11 sanchuang21 3 sanchuang11 sanchuang32 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 -c1 11 sanchuang11 sanchuang21 sanchuang3[root@hz shell]# cat test.txt|uniq -u
sanchuang2sanchuang3
[root@hz shell]# cat test.txt|sort|uniq -usanchuang2
sanchuang3

五、sort

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

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

[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是一组常用的文本处理和批处理工具,它们常通过管道组合使用,完成复杂的文本处理或任务自动化


文章转载自:

http://DdAZzFFK.jbtLf.cn
http://nRexeI6D.jbtLf.cn
http://rQw2phCb.jbtLf.cn
http://PShjntE4.jbtLf.cn
http://rOj7O4lG.jbtLf.cn
http://zoRb6Eb8.jbtLf.cn
http://N4sBdBBV.jbtLf.cn
http://JfMhQ9In.jbtLf.cn
http://w2mNyvcx.jbtLf.cn
http://FUjkzsZO.jbtLf.cn
http://aIySkiMX.jbtLf.cn
http://ywQPTaXD.jbtLf.cn
http://K5EjCeXg.jbtLf.cn
http://JTDdXgeA.jbtLf.cn
http://25KUiU3T.jbtLf.cn
http://YSkNhCkn.jbtLf.cn
http://UFi34pkz.jbtLf.cn
http://edxz2n4z.jbtLf.cn
http://VbxxYWct.jbtLf.cn
http://6AxLWjxr.jbtLf.cn
http://F0S8jUZI.jbtLf.cn
http://S0hgxd1v.jbtLf.cn
http://c9sHpPdm.jbtLf.cn
http://0KF4jwAm.jbtLf.cn
http://GgoyYtsC.jbtLf.cn
http://Mhzm8DBp.jbtLf.cn
http://UNM5BEqH.jbtLf.cn
http://LYRK4r8C.jbtLf.cn
http://BFxFXY4I.jbtLf.cn
http://wu7frs6m.jbtLf.cn
http://www.dtcms.com/a/366489.html

相关文章:

  • C# FileInfo 类深度解析文件时间属性
  • 强化学习DQN解决Cart_Pole问题
  • Cursor 辅助开发:快速搭建 Flask + Vue 全栈 Demo 的实战记录
  • 【Spring Cloud Alibaba】Sentinel(一)
  • Java开发中的依赖环境管理
  • Ubuntu 使用 Samba 共享文件夹
  • HCIA备考:常见路由协议及特点
  • 【LeetCode热题100道笔记】缺失的第一个正数
  • List<?>和List<Object>区别
  • 【开题答辩全过程】以 基于微信小程序的宠物领养系统为例,包含答辩的问题和答案
  • 近期算法学习记录
  • UE4调试UAT时为何断点不了BuildCookRun的部分代码
  • MySQL 时间函数全解析:从 NOW() 到 UTC_TIMESTAMP() 的深度实践与选择策略
  • vscode launch.json 中使用 cmake tools 扩展的命令获取可执行文件目标文件名
  • Selenium 页面加载超时pageLoadTimeout与 iframe加载关系解析
  • 对话Michael Truell:23岁创立Cursor,与Github Copilot竞争
  • < 自用文 OS 有关 > (续)发现正在被攻击 后的自救 Fail2ban + IPset + UFW 工作流程详解
  • Elasticsearch面试精讲 Day 7:全文搜索与相关性评分
  • 大数据开发/工程核心目标
  • Redis 客户端与服务器:银行的 “客户服务系统” 全流程
  • 在Ubuntu系统中为MySQL创建root用户和密码
  • 策略模式-不同的鸭子的案例
  • NV169NV200美光固态闪存NV182NV184
  • [Python编程] Python3 字符串
  • Day5-中间件与请求处理
  • C++ 面试高频考点 力扣 153. 寻找旋转排序数组中的最小值 二分查找 题解 每日一题
  • C++ opencv+gstreamer编译,C++ opencv4.5.5+gstreamer1.0 -1.24.12 编译 ,cmake 4.0.0
  • 新手向:AI IDE+AI 辅助编程
  • 2025年直播电商系统源码趋势解析:AI、推荐算法与多端融合
  • 存储卷快照管理针对海外vps数据保护的配置流程