当前位置:网站首页>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 {
} \;
边栏推荐
- Advanced Computer Network Review(3)——BBR
- MySQL uninstallation and installation methods
- [oc]- < getting started with UI> -- common controls - prompt dialog box and wait for the prompt (circle)
- UML图记忆技巧
- Intel distiller Toolkit - Quantitative implementation 2
- Leetcode: Jianzhi offer 04 Search in two-dimensional array
- LeetCode41——First Missing Positive——hashing in place & swap
- LeetCode41——First Missing Positive——hashing in place & swap
- Tdengine biweekly selection of community issues | phase III
- LeetCode:162. 寻找峰值
猜你喜欢

Intel distiller Toolkit - Quantitative implementation 2

LeetCode:221. Largest Square
![[MySQL] limit implements paging](/img/94/2e84a3878e10636460aa0fe0adef67.jpg)
[MySQL] limit implements paging

Intel distiller Toolkit - Quantitative implementation 1

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

LeetCode:236. The nearest common ancestor of binary tree

BN折叠及其量化

An article takes you to understand the working principle of selenium in detail

LeetCode:498. 对角线遍历

opencv+dlib实现给蒙娜丽莎“配”眼镜
随机推荐
【文本生成】论文合集推荐丨 斯坦福研究者引入时间控制方法 长文本生成更流畅
Super efficient! The secret of swagger Yapi
在QWidget上实现窗口阻塞
AcWing 2456. 记事本
[text generation] recommended in the collection of papers - Stanford researchers introduce time control methods to make long text generation more smooth
LeetCode:剑指 Offer 04. 二维数组中的查找
Booking of tourism products in Gansu quadrupled: "green horse" became popular, and one room of B & B around Gansu museum was hard to find
LeetCode:498. Diagonal traversal
In depth analysis and encapsulation call of requests
Ijcai2022 collection of papers (continuously updated)
[oc]- < getting started with UI> -- learning common controls
Using C language to complete a simple calculator (function pointer array and callback function)
自定义卷积注意力算子的CUDA实现
LeetCode:836. 矩形重叠
MongoDB 的安装和基本操作
Revit secondary development Hof method calls transaction
KDD 2022论文合集(持续更新中)
一改测试步骤代码就全写 为什么不试试用 Yaml实现数据驱动?
Problems encountered in connecting the database of the project and their solutions
Advanced Computer Network Review(3)——BBR