当前位置:网站首页>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:剑指 Offer 03. 数组中重复的数字
- What is the role of automated testing frameworks? Shanghai professional third-party software testing company Amway
- Intel distiller Toolkit - Quantitative implementation 3
- UML圖記憶技巧
- LeetCode:39. Combined sum
- Pytest parameterization some tips you don't know / pytest you don't know
- I-BERT
- Pytorch view tensor memory size
- LeetCode:124. 二叉树中的最大路径和
- 不同的数据驱动代码执行相同的测试场景
猜你喜欢
LeetCode:236. 二叉树的最近公共祖先
LeetCode41——First Missing Positive——hashing in place & swap
Booking of tourism products in Gansu quadrupled: "green horse" became popular, and one room of B & B around Gansu museum was hard to find
Opencv+dlib realizes "matching" glasses for Mona Lisa
Simclr: comparative learning in NLP
不同的数据驱动代码执行相同的测试场景
The carousel component of ant design calls prev and next methods in TS (typescript) environment
Pytest之收集用例规则与运行指定用例
Redis之Bitmap
UML圖記憶技巧
随机推荐
Parameterization of postman
[OC-Foundation框架]--<Copy对象复制>
[MySQL] limit implements paging
Selenium+pytest automated test framework practice
Redis之Bitmap
Leetcode: Jianzhi offer 03 Duplicate numbers in array
Leetcode刷题题解2.1.1
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
Niuke winter vacation training 6 maze 2
LeetCode:剑指 Offer 03. 数组中重复的数字
Advanced Computer Network Review(3)——BBR
After reading the programmer's story, I can't help covering my chest...
BN折叠及其量化
UML圖記憶技巧
BMINF的後訓練量化實現
LeetCode:673. 最长递增子序列的个数
Opencv+dlib realizes "matching" glasses for Mona Lisa
[OC]-<UI入门>--常用控件-UIButton
Export IEEE document format using latex
Leetcode problem solving 2.1.1