当前位置:网站首页>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 {
} \;
边栏推荐
- [text generation] recommended in the collection of papers - Stanford researchers introduce time control methods to make long text generation more smooth
- [OC]-<UI入门>--常用控件的学习
- Tdengine biweekly selection of community issues | phase III
- What are the common processes of software stress testing? Professional software test reports issued by companies to share
- LeetCode:26. Remove duplicates from an ordered array
- @Jsonbackreference and @jsonmanagedreference (solve infinite recursion caused by bidirectional references in objects)
- LeetCode41——First Missing Positive——hashing in place & swap
- vb.net 随窗口改变,缩放控件大小以及保持相对位置
- Improved deep embedded clustering with local structure preservation (Idec)
- LeetCode:673. 最长递增子序列的个数
猜你喜欢

UML图记忆技巧

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

Using C language to complete a simple calculator (function pointer array and callback function)

Computer graduation design PHP Zhiduo online learning platform
![[OC-Foundation框架]--<Copy对象复制>](/img/62/c04eb2736c2184d8826271781ac7e3.png)
[OC-Foundation框架]--<Copy对象复制>
![[MySQL] multi table query](/img/eb/9d54df9a5c6aef44e35c7a63b286a6.jpg)
[MySQL] multi table query

CUDA implementation of self defined convolution attention operator

MYSQL卸载方法与安装方法

使用latex导出IEEE文献格式

Using pkgbuild:: find in R language_ Rtools check whether rtools is available and use sys The which function checks whether make exists, installs it if not, and binds R and rtools with the writelines
随机推荐
vb.net 随窗口改变,缩放控件大小以及保持相对位置
注意力机制的一种卷积替代方式
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
Revit secondary development Hof method calls transaction
LeetCode:26. 删除有序数组中的重复项
Tdengine biweekly selection of community issues | phase III
【嵌入式】使用JLINK RTT打印log
Pytest参数化你不知道的一些使用技巧 /你不知道的pytest
LeetCode:836. 矩形重叠
LeetCode:124. Maximum path sum in binary tree
可变长参数
[oc foundation framework] - < copy object copy >
R language ggplot2 visualization: place the title of the visualization image in the upper left corner of the image (customize Title position in top left of ggplot2 graph)
Philosophical enlightenment from single point to distributed
LeetCode:劍指 Offer 42. 連續子數組的最大和
CSP first week of question brushing
Unsupported operation exception
LeetCode:剑指 Offer 48. 最长不含重复字符的子字符串
LeetCode:394. String decoding
Digital people anchor 618 sign language with goods, convenient for 27.8 million people with hearing impairment