xargs、sort、uniq命令

xargs、sort、uniq命令,我们由LeetCode的一道题来引入,并使用加以理解;

题目是这样的:写一个 bash 脚本以统计一个文本文件 words.txt 中每个单词出现的频率。

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 将 \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

原文地址:https://www.cnblogs.com/guanbin-529/p/11397377.html

时间: 2024-08-26 07:18:41

xargs、sort、uniq命令的相关文章

linux sort uniq命令详解

sort 功能说明:将文本文件内容加以排序,sort可针对文本文件的内容,以行为单位来排序. sort [-bcdfimMnr][-o<输出文件>][-t<分隔字符>][+<起始栏位>-<结束栏位>][--help][--verison][文件] -b   忽略每行前面开始处的空格字符 . -c   检查文件是否已经按照顺序排序. -d   排序时,处理英文字母.数字及空格字符外,忽略其他的字符. -f   排序时,将小写字母视为大写字母. -i   排序时

Linux sort uniq 命令。简单运用

-n                              #代表以数字方法排序,如果倒序加上-r -t ':'                          #-t指定分隔符-k                           #指定第几列 ---------------------------------------------------------------------- 文本如下: root:x:0:0:root:/root:/bin/bashbin:x:1:1:bin:

Linux管线命令 - cut,grep,sort,uniq,wc,tee,tr,col,join,paste,expand,split,xargs

在每个管线后面接的第一个数据必定是『命令』喔!而且这个命令必须要能够接受 standard input 的数据才行,这样的命令才可以是为『管线命令』,例如 less, more, head, tail 等都是可以接受 standard input 的管线命令啦.至于例如 ls, cp, mv 等就不是管线命令了!因为 ls, cp, mv 并不会接受来自 stdin 的数据. 也就是说,管线命令主要有两个比较需要注意的地方: 管线命令仅会处理 standard output,对于 standar

sort,uniq,cut,wc命令详解

sortsort 命令对 File 参数指定的文件中的行排序,并将结果写到标准输出.如果 File 参数指定多个文件,那么 sort 命令将这些文件连接起来,并当作一个文件进行排序. sort语法 sort [-fbMnrtuk] [file or stdin]选项与参数:-f  :忽略大小写的差异,例如 A 与 a 视为编码相同:-b  :忽略最前面的空格符部分:-M  :以月份的名字来排序,例如 JAN, DEC 等等的排序方法:-n  :使用『纯数字』进行排序(默认是以文字型态来排序的):

Linux基础之文本处理命令(wc,cut,sort,uniq,diff,patch)

我相信大家在使用Linux过程中总会遇到想要提取某些自己需要的信息的情况,比如如下这四种情况: 1.找出ifconfig命令结果中eno16777728的IPv4地址 2.查出分区空间使用率的最大百分比值 3.查出/tmp的权限,以数字方式显示 这个时候,我们使用命令当然也可以查看,不过还需要自己通过眼睛去过滤不需要的信息,多费劲.如何让自己更轻松的看到自己想看到自己想看的信息呢?今天的文本处理命令能满足我们的简单需求. wc 此wc非彼WC,在这里wc是word count的简写 wc - p

linux cat,tac,more,less,head,tail,cut,sort,uniq,wc,tr命令的使用

cat:连接并显示,比如: [[email protected] ~]# cat /tmp/sort.test  111 324 567 324 890 890 567 abc 加上选项-n会显示行号: [[email protected] ~]# cat -n /tmp/sort.test      1111      2324      3567      4324      5890      6890      7567      8abc 加上-E选项会显示行尾符$:(linux中行尾

cat &nbsp; cut &nbsp; paste sort uniq diff 等命令用法

cat命令: cat命令连接文件并打印到标准输出设备上,cat经常用来显示文件的内容. cat  选项  参数 -n  :给所有输出的行数编号 -E  :显示每行的行结束符 -T  :制表符 -v  :显示非打印字符 -s  :当遇到有连续两行以上的空白行,就代换为一行的空白行 tac与cat反向显示 rve行内反着显示 例: [[email protected] ~]# rev /etc/passwd hsab/nib/:toor/:toor:0:0:x:toor 2.less命令: less

sort排序命令 uniq 去除排序过的文件中的重复行 cut提取命令 wc 统计命令

sort 命令对 File 参数指定的文件中的行排序,并将结果写到标准输出.如果 File 参数指定多个文件,那么 sort 命令将这些文件连接起来,并当作一个文件进行排序. sort语法 [[email protected] ~]# sort [-fbMnrtuk] [file or stdin] 选项与参数: -f :忽略大小写的差异,例如 A 与 a 视为编码相同: -b :忽略最前面的空格符部分: -M :以月份的名字来排序,例如 JAN, DEC 等等的排序方法: -n :使用『纯数字

linux系统中cut sort tee 和uniq命令的使用

cut命令的使用 -----分割  [[email protected] wang]# cut -d ':' -f 1 passwd1 |head -n3  //cut命令用于切割.-d后面指  定分隔符,用单引号引起来  root                                                           //-f指定第几段.即用分隔符分割后的第几段  bin daemon   [[email protected] wang]# cut -c1 pass