xargs、sort、uniq命令
xargs、sort、uniq命令,我们由LeetCode的一道题来引入,并使用加以理解; 题目是这样的:写一个 bash 脚本以统计一个文本文件? words.txt的内容为: the day is sunny the the the sunny is is 1.cat words.txt | sort 来看下会是什么效果[[email?protected] tmp]# cat words.txt | sort the day is sunny the the the sunny is is ? sort 命令将以默认的方式将文本文件的第一列以ASCII 码的次序排列,并将结果输出到标准输出。 ?2.使用 cat words.txt | xargs -n1 | sort | uniq -c? ?看下是什么效果[[email?protected] tmp]# cat words.txt | xargs -n1 | sort | uniq -c 1 day 3 is 2 sunny 4 the ?uniq命令只能对相邻行进行去重复操作,所以在进行去重前,先要对文本行进行排序,使重复行集中到一起,这就是为什么要先sort的原因;-c 是统计数量 ? 3.使用?cat words.txt | xargs -n1 | sort | uniq -c? | sort -nr 看下是什么效果[[email?protected] tmp]# cat words.txt | xargs -n1 | sort | uniq -c | sort -n 1 day 2 sunny 3 is 4 the [[email?protected]-n93yom tmp]# cat words.txt | xargs -n1 | sort | uniq -c | sort -nr 4 the 3 is 2 sunny 1 day ? ? sort -n 是按照数字进行排序,默认是升序的,-r是降序,所以sort -nr就是按照数字进行降序排序 ? ?4. 再结合上awk命令输出结果即可:? ? cat words.txt | xargs -n1 | sort | uniq -c | sort -nr | awk ‘{print $2" "$1}‘ [[email?protected] tmp]# cat words.txt | xargs -n1 | sort | uniq -c | sort -nr | awk ‘{print $2" "$1}‘ the 4 is 3 sunny 2 day 1 ? 下面介绍下xargs命令:? ?1. cat words.txt | xargs 是把文本变为一行输出? ? [[email?protected] tmp]# cat words.txt | xargs the day is sunny the the the sunny is is ? ? 加上-n 数字? 把文本变成多行输出,数字是每行的单词数 [[email?protected] tmp]# cat words.txt | xargs -n1 the day is sunny the the the sunny is is ?xargs -dx 指定分隔符进行输出 [[email?protected] tmp]# echo "nameXnameXnameXname" | xargs -dX name name name name ? 定义一个脚本sk.sh #!/bin/bash
#sk.sh命令内容,打印出所有参数。
echo $*
然后执行命令:cat words.txt | xargs -I {} ./sk.sh -p {} -l xargs 的一个选项 -I,使用 -I 指定一个替换字符串 {},这个字符串在 xargs 扩展时会被替换掉,当 -I 与 xargs 结合使用,每一个参数命令都会被执行一次: [[email?protected] tmp]# cat words.txt | xargs -I {} ./sk.sh -p {} -l -p the day is sunny the the -l -p the sunny is is -l 把当前文件夹下所有的.sh文件copy到tmp文件下 ?ls *.sh | xargs -n1 -I {} cp {} /root/tmp/ [[email?protected] ~]# ll total 20-rw-r--r-- 1 root root 49 Aug 15 17:01 mysql.sh-rw-r--r-- 1 root root 640 Aug 15 17:13 upgrade.sh [[email?protected]-n93yom ~]# ls *.sh | xargs -n1 -I {} cp {} /root/tmp/ [[email?protected]-n93yom ~]# cd tmp/ [[email?protected]-n93yom tmp]# ll total 20 -rw-r--r-- 1 root root 49 Aug 22 22:53 mysql.sh -rw-r--r-- 1 root root 1559 Aug 19 23:13 passwd -rwxr-xr-x 1 root root 21 Aug 22 22:44 sk.sh -rw-r--r-- 1 root root 640 Aug 22 22:53 upgrade.sh -rw-r--r-- 1 root root 41 Aug 22 14:03 words.txt ? xargs 结合 find 使用? 用 rm 删除太多的文件时候,可能得到一个错误信息:/bin/rm Argument list too long. 用 xargs 去避免这个问题:
find . -type f -name "*.log" -print0 | xargs -0 rm -f xargs -0 将 作为定界符。 统计一个源代码目录中所有 php 文件的行数: find . -type f -name "*.php" -print0 | xargs -0 wc -l 查找所有的 jpg 文件,并且压缩它们: find . -type f -name "*.jpg" -print | xargs tar -czvf images.tar.gz xargs 其他应用 假如你有一个文件包含了很多你希望下载的 URL,你能够使用 xargs下载所有链接: # cat url-list.txt | xargs wget -c
? 参考:https://www.runoob.com/linux/linux-comm-xargs.html (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |