xargs
例如我们有这样一个需求
将whereis 查出来的路径传递给ls -l 列出详情
命令 | 选项 | 参数 |
---|---|---|
ls | -l | \boot |
正常情况下管道未将whereis的结果作为参数传递到 ls 的参数
[root@localhost /]# whereis mkdir |ls
backup boot etc lib64 mnt root srv tmp
backup_db dbbackup home mariadb_log_file opt run sys usr
bin dev lib media proc sbin TAG var
使用xargs可以实现我们想要的效果.
将前面拿到的值专门作为参数使用
[root@localhost ~]# whereis mkdir |xargs ls -l
-rwxr-xr-x. 1 root root 79768 Aug 20 2019 /usr/bin/mkdir
-rw-r--r--. 1 root root 989 Aug 20 2019 /usr/share/man/man1/mkdir.1.gz
seq
产生一个序列
-w 参数 等宽
[root@localhost ~]# seq 5
1
2
3
4
5
[root@localhost ~]# seq 5 10
5
6
7
8
9
10
[root@localhost ~]# seq -w 5 10
05
06
07
08
09
10
[root@localhost ~]# seq 10 -2 5
10
8
6
[root@localhost ~]# seq 10 -1 5
10
9
8
7
6
5
在shell中的应用场景
[root@localhost 2025-1-14]# cat for_touch.sh
#!/bin/bash
num=$1
filename=$2for i in $(seq $num)
dotouch ${filename}_$i.txtdone
[root@localhost 2025-1-14]# bash for_touch.sh 5 luobozi
[root@localhost 2025-1-14]# ls
for_touch.sh luobozi_2.txt luobozi_4.txt
luobozi_1.txt luobozi_3.txt luobozi_5.txt
tr
- 转换字符串
tr [option] set1 [set2]
abc 123
将a转化为1
将b转化为2
将c转化为3 - 删除字符串
-d --delete - 压缩字符串
-s
去重 将连续的重复字符串压缩成一个
[root@localhost ~]# echo "hello,world" |tr -d "o"
hell,wrld[root@localhost ~]# df |tr -d "%" #将符号删除
Filesystem 1K-blocks Used Available Use Mounted on
devtmpfs 919504 0 919504 0 /dev
tmpfs 931516 0 931516 0 /dev/shm
tmpfs 931516 9792 921724 2 /run
tmpfs 931516 0 931516 0 /sys/fs/cgroup
/dev/mapper/centos-root 17811456 2773304 15038152 16 /
/dev/sda1 1038336 153688 884648 15 /boot
tmpfs 186304 0 186304 0 /run/user/0[root@localhost ~]# echo "abcabcdefabcabcaaaaaaaaabbbbbbvvvcccc" |tr abc 123 #将一个集合中的元组转换成另一个集合元素
123123def123123111111111222222vvv3333[root@localhost ~]# cat /etc/hosts |tr "[a-z]" "[A-Z]" #小写转换成大写
127.0.0.1 LOCALHOST LOCALHOST.LOCALDOMAIN LOCALHOST4 LOCALHOST4.LOCALDOMAIN4
::1 LOCALHOST LOCALHOST.LOCALDOMAIN LOCALHOST6 LOCALHOST6.LOCALDOMAIN6[root@localhost ~]# cat /etc/hosts |tr -d "[0-9]" #将数字删除
... localhost localhost.localdomain localhost localhost.localdomain
:: localhost localhost.localdomain localhost localhost.localdomain[root@localhost ~]# echo "11112223333" |tr -s 13
12223[root@localhost ~]# df |tr -s " " #将空格去重
Filesystem 1K-blocks Used Available Use% Mounted on
devtmpfs 919504 0 919504 0% /dev
tmpfs 931516 0 931516 0% /dev/shm
tmpfs 931516 9792 921724 2% /run
tmpfs 931516 0 931516 0% /sys/fs/cgroup
/dev/mapper/centos-root 17811456 2773312 15038144 16% /
/dev/sda1 1038336 153688 884648 15% /boot
tmpfs 186304 0 186304 0% /run/user/0
uniq
uniq - report or omit repeated lines
去除重复的行
- 统计重复的次数
-c
选项 count - 只显示出现了一次的行
-u
[root@localhost ~]# cat test.txt
hello,world1
hello,world1
hello,world1
hello,world1
hello,world1
hello,world1
hello,world1
hello,world1hello,world1
hello,world1
hello,world2
hello,world1
hello,world1
hello,world3
hello,world1
hello,world1
hello,world1
[root@localhost ~]# cat test.txt |uniq #去重连续重复的行
hello,world1hello,world1
hello,world2
hello,world1
hello,world3
hello,world1
[root@localhost ~]# cat test.txt |uniq -c #统计出现的次数8 hello,world122 hello,world11 hello,world22 hello,world11 hello,world33 hello,world1
[root@localhost ~]# cat test.txt |uniq -u #只显示唯一的行
hello,world2
hello,world3
sort
排序的命令,默认情况下是根据字符的ASCII码升序
-n 将数字当作一个整体的数值来比较,默认升序
-r 反转 – 降序
-k 指定哪一列进行排序
-t 指定分隔符
#按照passwd 里面的uid降序排序 sort -t ":" -k 3 -n -r
cat /etc/passwd |sort -t ":" -k 3 -n -r |tr ":" "\t"nobody x 65534 65534 nobody /nonexistent /usr/sbin/nologin
lxd x 999 100 /var/snap/lxd/common/lxd /bin/false
glances x 112 120 /var/lib/glances /usr/sbin/nologin
sshd x 109 65534 /run/sshd /usr/sbin/nologin
tcpdump x 108 114 /nonexistent /usr/sbin/nologin
uuidd x 107 113 /run/uuidd /usr/sbin/nologin
tss x 106 112 TPM software stack,,, /var/lib/tpm /bin/false
_apt x 105 65534 /nonexistent /usr/sbin/nologin
syslog x 104 111 /home/syslog /usr/sbin/nologin
经典面试题
统计nginx日志中出现次数前五的IP(sort + uniq)
awk '{print $1}' access.log |sort |uniq -c |sort -rn | head -5
root@kvm-lenoud:/mnt/lv_home/nginx/log# awk '{print $1}' access.log |sort |uniq -c |sort -rn | head -55580 141.98.11.1141663 192.168.193.54267 185.191.126.213254 3.211.13.218254 206.189.136.214