当前位置:网站首页>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 {
} \;
边栏推荐
- Leetcode: Jianzhi offer 03 Duplicate numbers in array
- [oc]- < getting started with UI> -- learning common controls
- LeetCode:剑指 Offer 48. 最长不含重复字符的子字符串
- Compétences en mémoire des graphiques UML
- UML图记忆技巧
- 【嵌入式】Cortex M4F DSP库
- UnsupportedOperationException异常
- [sword finger offer] serialized binary tree
- LeetCode:394. String decoding
- R language ggplot2 visualization, custom ggplot2 visualization image legend background color of legend
猜你喜欢

可变长参数

Selenium+Pytest自动化测试框架实战

Nacos 的安装与服务的注册

KDD 2022论文合集(持续更新中)

Alibaba cloud server mining virus solution (practiced)
![[oc]- < getting started with UI> -- common controls uibutton](/img/4d/f5a62671068b26ef43f1101981c7bb.png)
[oc]- < getting started with UI> -- common controls uibutton
![[OC foundation framework] - [set array]](/img/b5/5e49ab9d026c60816f90f0c47b2ad8.png)
[OC foundation framework] - [set array]

Detailed explanation of dynamic planning

LeetCode:498. 对角线遍历

广州推进儿童友好城市建设,将探索学校周边200米设安全区域
随机推荐
什么是MySQL?MySql的学习之路是怎样的
TP-LINK enterprise router PPTP configuration
Super efficient! The secret of swagger Yapi
vb.net 随窗口改变,缩放控件大小以及保持相对位置
LeetCode:673. Number of longest increasing subsequences
Leetcode: Jianzhi offer 04 Search in two-dimensional array
R language ggplot2 visualization, custom ggplot2 visualization image legend background color of legend
Pytorch view tensor memory size
Li Kou daily question 1 (2)
UML圖記憶技巧
After reading the programmer's story, I can't help covering my chest...
Pytest之收集用例规则与运行指定用例
[today in history] February 13: the father of transistors was born The 20th anniversary of net; Agile software development manifesto was born
[oc]- < getting started with UI> -- common controls - prompt dialog box and wait for the prompt (circle)
使用标签模板解决用户恶意输入的问题
自定义卷积注意力算子的CUDA实现
TDengine 社区问题双周精选 | 第三期
LeetCode:39. 组合总和
【文本生成】论文合集推荐丨 斯坦福研究者引入时间控制方法 长文本生成更流畅
Guangzhou will promote the construction of a child friendly city, and will explore the establishment of a safe area 200 meters around the school