当前位置:网站首页>Basic usage of xargs command
Basic usage of xargs command
2022-07-06 09:10:00 【chaolei_ nine thousand five hundred and twenty-seven】
effect
For some commands , Data can only be accepted as command line arguments , And not through stdin Accept the data stream . In this case , There is no way to pipeline data to commands that can only accept command line parameters . The pipeline is only applicable to acceptable stdin As a command of data flow .
xargs The command should immediately follow the pipeline operator , Take standard input as the main source data , Use stdin Execute other commands as command line parameters .
usage
xargs Common command options ( Command line arguments ) Yes :
-n column : Specify the number of output columns
-d flag : Specify a delimiter for the data source , Split data sources
-I {
} : Place the data source in the specified location after conversion
When xargs Don't specify -n column Option , that xargs Will stdin coming ( Pipeline symbol implementation ) The source data of is converted into a row , And each parameter is separated by a space , And if you specify -n Option, the source data will be divided into the number of columns corresponding to the following numbers , Similarly, each parameter is separated by spaces . for example :
1) xargs General use of
# example.txt
1 2 3 4
5 6 7
8 9
# Convert the data source to 1 That's ok
cat example.txt | xargs
1 2 3 4 5 6 7 8 9
# Convert the data source to 2 Column output
cat example.txt | xargs -n 2
1 2
3 4
5 6
7 8
9
2) Use of delimiters
# Use X Split data sources , because xargs After processing, the space is used as the delimiter segmentation result
echo "splitXsplitXsplitXsplit" | xargs -d X
split split split split
3) Advanced use
1. Read stdin, Pass formatting parameters to the command
# The test script ceshi.sh
#!/bin/bash
echo $* '#'
# args.txt
arg1
arg2
arg3
## Read stdin, Pass formatting parameters to the command
cat args.txt | xargs -n 2 ./ceshi.sh
arg1 arg2 #
arg3 #
2. Place the data source in a specific location
for example , You want to provide the data source to the command in the following form :
./ceshi.sh -p arg1 -a
./ceshi.sh -p arg2 -a
./ceshi.sh -p arg3 -a
That is, you need to specify parameters in -p After the options , At this time, how to use xargs Orders ?
When you need to convert stdin When the source data goes to a specific command line location , have access to xargs Ordered -I Options , stay -I Specify placeholder after option , Then use this placeholder after a specific option , It means that we will start from stdin The converted data will be placed in this location . for example
cat args.txt | xargs -I {
} ./ceshi.sh -p {
} -a
# The following commands have the same effect as the above commands , But another placeholder is specified ,
cat args.txt | xargs -I [] ./ceshi.sh -p [] -a
- and find Use it together
Here are a few simple examples .
# 1. remove the current directory txt Final document
find . -type f -name "*.txt" | xargs rm -f
# 2. Count the number of lines of each file in the current directory
find . -type f -name "*.txt" | xargs wc -l
# Equivalent to the following command
find . -type f -name "*.txt" -exec wc -l {
} \;
边栏推荐
- LeetCode:26. 删除有序数组中的重复项
- opencv+dlib实现给蒙娜丽莎“配”眼镜
- 【图的三大存储方式】只会用邻接矩阵就out了
- [oc foundation framework] - < copy object copy >
- LeetCode:498. Diagonal traversal
- Selenium+pytest automated test framework practice
- ant-design的走马灯(Carousel)组件在TS(typescript)环境中调用prev以及next方法
- Pytorch view tensor memory size
- How to effectively conduct automated testing?
- Pytest之收集用例规则与运行指定用例
猜你喜欢
Once you change the test steps, write all the code. Why not try yaml to realize data-driven?
Problems encountered in connecting the database of the project and their solutions
LeetCode:124. 二叉树中的最大路径和
Compétences en mémoire des graphiques UML
requests的深入刨析及封装调用
注意力机制的一种卷积替代方式
Computer graduation design PHP Zhiduo online learning platform
[OC foundation framework] - [set array]
Advance Computer Network Review(1)——FatTree
Advanced Computer Network Review(3)——BBR
随机推荐
Advanced Computer Network Review(4)——Congestion Control of MPTCP
Alibaba cloud server mining virus solution (practiced)
Compétences en mémoire des graphiques UML
BMINF的後訓練量化實現
如何正确截取字符串(例:应用报错信息截取入库操作)
[OC]-<UI入门>--常用控件-提示对话框 And 等待提示器(圈)
How to effectively conduct automated testing?
Li Kou daily question 1 (2)
Computer graduation design PHP Zhiduo online learning platform
[OC-Foundation框架]---【集合数组】
Once you change the test steps, write all the code. Why not try yaml to realize data-driven?
Selenium+Pytest自动化测试框架实战(下)
LeetCode:26. Remove duplicates from an ordered array
Leetcode: Jianzhi offer 03 Duplicate numbers in array
Opencv+dlib realizes "matching" glasses for Mona Lisa
Leetcode: Jianzhi offer 04 Search in two-dimensional array
[oc foundation framework] - < copy object copy >
Ijcai2022 collection of papers (continuously updated)
LeetCode:673. 最长递增子序列的个数
[Hacker News Weekly] data visualization artifact; Top 10 Web hacker technologies; Postman supports grpc