当前位置:网站首页>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 {
} \;
边栏推荐
- CUDA realizes focal_ loss
- I-BERT
- LeetCode:34. Find the first and last positions of elements in a sorted array
- LeetCode:剑指 Offer 48. 最长不含重复字符的子字符串
- Intel Distiller工具包-量化实现1
- 随手记01
- LeetCode:124. Maximum path sum in binary tree
- TP-LINK enterprise router PPTP configuration
- LeetCode:221. Largest Square
- LeetCode:214. Shortest palindrome string
猜你喜欢
随机推荐
LeetCode:34. Find the first and last positions of elements in a sorted array
【文本生成】论文合集推荐丨 斯坦福研究者引入时间控制方法 长文本生成更流畅
What are the common processes of software stress testing? Professional software test reports issued by companies to share
LeetCode:41. Missing first positive number
Pytest参数化你不知道的一些使用技巧 /你不知道的pytest
[embedded] cortex m4f DSP Library
Philosophical enlightenment from single point to distributed
Implement window blocking on QWidget
pytorch查看张量占用内存大小
CUDA实现focal_loss
LeetCode:394. String decoding
R language uses the principal function of psych package to perform principal component analysis on the specified data set. PCA performs data dimensionality reduction (input as correlation matrix), cus
LeetCode:34. 在排序数组中查找元素的第一个和最后一个位置
Using label template to solve the problem of malicious input by users
Using C language to complete a simple calculator (function pointer array and callback function)
Pytorch view tensor memory size
Digital people anchor 618 sign language with goods, convenient for 27.8 million people with hearing impairment
LeetCode:剑指 Offer 42. 连续子数组的最大和
LeetCode:673. Number of longest increasing subsequences
Post training quantification of bminf