当前位置:网站首页>xargs命令的基本用法
xargs命令的基本用法
2022-07-06 08:58:00 【chaolei_9527】
作用
对于某些命令,只能以命令行参数的形式接受数据,而无法通过stdin接受数据流。这种情况下,没有办法通过管道给那些只能接受命令行参数的命令提供数据。而管道只适用于能接受stdin作为数据流的命令。
xargs命令应该紧跟在管道操作符之后,以标准输入作为主要的源数据,使用stdin作为命令行参数来执行其他命令。
用法
xargs命令常用的选项(命令行参数)有:
-n column : 指定输出列数
-d flag : 为数据源指定定界符,分割数据源
-I {
} : 将数据源转换后放置在指定位置
当xargs不指定-n column 选项时,那么xargs会将stdin传过来(管道符实现)的源数据转换成一行,并且每个参数由空格隔开,而如果指定-n选项那么就会将源数据分成后面数字对应的列数,同样每个参数都是由空格进行分割。例如:
1) xargs的一般使用
# example.txt
1 2 3 4
5 6 7
8 9
# 将数据源转换成1行
cat example.txt | xargs
1 2 3 4 5 6 7 8 9
# 将数据源转换成2列输出
cat example.txt | xargs -n 2
1 2
3 4
5 6
7 8
9
2)定界符的使用
# 使用X分割数据源,由于xargs处理以后都是以空格作为定界符分割结果
echo "splitXsplitXsplitXsplit" | xargs -d X
split split split split
3)高阶使用
1.读取stdin,将格式化参数传递给命令
# 测试脚本 ceshi.sh
#!/bin/bash
echo $* '#'
# args.txt
arg1
arg2
arg3
## 读取stdin,将格式化参数传递给命令
cat args.txt | xargs -n 2 ./ceshi.sh
arg1 arg2 #
arg3 #
2.将数据源放置在特定位置
例如,希望以如下形式将数据源提供给命令:
./ceshi.sh -p arg1 -a
./ceshi.sh -p arg2 -a
./ceshi.sh -p arg3 -a
即需要将参数指定放在-p选项后,此时又该如何使用xargs命令呢?
当需要转换stdin源数据到特定命令行位置时,可以使用xargs命令的-I选项,在-I选项后指定占位符,然后在特定选项后使用该占位符,就表示将从stdin转换的数据将被放置在该位置。例如
cat args.txt | xargs -I {
} ./ceshi.sh -p {
} -a
# 下面的命令和上面的命令效果一样,却指定了另一个占位符,
cat args.txt | xargs -I [] ./ceshi.sh -p [] -a
- 和find一起使用
这里提供几个简单的示例。
# 1.删除当前目录txt结尾的文件
find . -type f -name "*.txt" | xargs rm -f
# 2.统计当前目录各文件的行数
find . -type f -name "*.txt" | xargs wc -l
# 等同于下面的命令
find . -type f -name "*.txt" -exec wc -l {
} \;
边栏推荐
- 在QWidget上实现窗口阻塞
- LeetCode:39. Combined sum
- Intel distiller Toolkit - Quantitative implementation 1
- LeetCode:124. Maximum path sum in binary tree
- Mongodb installation and basic operation
- What is an R-value reference and what is the difference between it and an l-value?
- LeetCode:221. 最大正方形
- [oc]- < getting started with UI> -- common controls uibutton
- Tdengine biweekly selection of community issues | phase III
- Alibaba cloud server mining virus solution (practiced)
猜你喜欢
Alibaba cloud server mining virus solution (practiced)
Compétences en mémoire des graphiques UML
Mongodb installation and basic operation
Variable length parameter
Detailed explanation of dynamic planning
TP-LINK 企业路由器 PPTP 配置
LeetCode:498. Diagonal traversal
Mise en œuvre de la quantification post - formation du bminf
一篇文章带你了解-selenium工作原理详解
Simple use of promise in uniapp
随机推荐
Leetcode: Jianzhi offer 03 Duplicate numbers in array
opencv+dlib实现给蒙娜丽莎“配”眼镜
LeetCode:劍指 Offer 42. 連續子數組的最大和
BMINF的後訓練量化實現
CUDA realizes focal_ loss
BN folding and its quantification
Advance Computer Network Review(1)——FatTree
vb. Net changes with the window, scales the size of the control and maintains its relative position
Simple use of promise in uniapp
LeetCode:39. Combined sum
pytorch查看张量占用内存大小
[OC]-<UI入门>--常用控件-提示对话框 And 等待提示器(圈)
A convolution substitution of attention mechanism
注意力机制的一种卷积替代方式
Ijcai2022 collection of papers (continuously updated)
[embedded] cortex m4f DSP Library
【文本生成】论文合集推荐丨 斯坦福研究者引入时间控制方法 长文本生成更流畅
LeetCode:236. The nearest common ancestor of binary tree
Leetcode: Sword Finger offer 42. Somme maximale des sous - tableaux consécutifs
Variable length parameter