当前位置:网站首页>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:221. Largest Square
- Detailed explanation of dynamic planning
- Improved deep embedded clustering with local structure preservation (Idec)
- postman之参数化详解
- 【嵌入式】Cortex M4F DSP库
- 数学建模2004B题(输电问题)
- BN folding and its quantification
- UnsupportedOperationException异常
- What are the common processes of software stress testing? Professional software test reports issued by companies to share
- Esp8266-rtos IOT development
猜你喜欢

Mise en œuvre de la quantification post - formation du bminf

After reading the programmer's story, I can't help covering my chest...

Simple use of promise in uniapp
![[OC]-<UI入门>--常用控件的学习](/img/2c/d317166e90e1efb142b11d4ed9acb7.png)
[OC]-<UI入门>--常用控件的学习

Pytest参数化你不知道的一些使用技巧 /你不知道的pytest
![[text generation] recommended in the collection of papers - Stanford researchers introduce time control methods to make long text generation more smooth](/img/10/c0545cb34621ad4c6fdb5d26b495ee.jpg)
[text generation] recommended in the collection of papers - Stanford researchers introduce time control methods to make long text generation more smooth

SimCLR:NLP中的对比学习
![[oc]- < getting started with UI> -- common controls uibutton](/img/4d/f5a62671068b26ef43f1101981c7bb.png)
[oc]- < getting started with UI> -- common controls uibutton

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

Detailed explanation of dynamic planning
随机推荐
Intel distiller Toolkit - Quantitative implementation 3
Intel Distiller工具包-量化实现2
使用标签模板解决用户恶意输入的问题
LeetCode:673. Number of longest increasing subsequences
Guangzhou will promote the construction of a child friendly city, and will explore the establishment of a safe area 200 meters around the school
LeetCode:39. 组合总和
[oc foundation framework] - < copy object copy >
LeetCode:673. 最长递增子序列的个数
I-BERT
[embedded] cortex m4f DSP Library
Chapter 1 :Application of Artificial intelligence in Drug Design:Opportunity and Challenges
pytorch查看张量占用内存大小
Nacos 的安装与服务的注册
Export IEEE document format using latex
Advance Computer Network Review(1)——FatTree
Esp8266-rtos IOT development
[OC foundation framework] - [set array]
Problems encountered in connecting the database of the project and their solutions
What is the role of automated testing frameworks? Shanghai professional third-party software testing company Amway
Unsupported operation exception