当前位置:网站首页>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:498. 对角线遍历
- Li Kou daily question 1 (2)
- Advance Computer Network Review(1)——FatTree
- Philosophical enlightenment from single point to distributed
- LeetCode:394. String decoding
- 项目连接数据库遇到的问题及解决
- LeetCode:673. Number of longest increasing subsequences
- LeetCode:41. Missing first positive number
- LeetCode:387. 字符串中的第一个唯一字符
- LeetCode:836. Rectangle overlap
猜你喜欢

LeetCode:221. 最大正方形
![[OC foundation framework] - [set array]](/img/b5/5e49ab9d026c60816f90f0c47b2ad8.png)
[OC foundation framework] - [set array]

Advanced Computer Network Review(4)——Congestion Control of MPTCP
![[oc]- < getting started with UI> -- common controls - prompt dialog box and wait for the prompt (circle)](/img/af/a44c2845c254e4f48abde013344c2b.png)
[oc]- < getting started with UI> -- common controls - prompt dialog box and wait for the prompt (circle)

Compétences en mémoire des graphiques UML
![[text generation] recommended in the collection of papers - Stanford researchers introduce time control methods to make long text generation more smooth](/img/10/c0545cb34621ad4c6fdb5d26b495ee.jpg)
[text generation] recommended in the collection of papers - Stanford researchers introduce time control methods to make long text generation more smooth

Pytest parameterization some tips you don't know / pytest you don't know

【文本生成】论文合集推荐丨 斯坦福研究者引入时间控制方法 长文本生成更流畅

LeetCode:236. The nearest common ancestor of binary tree
![[OC-Foundation框架]---【集合数组】](/img/b5/5e49ab9d026c60816f90f0c47b2ad8.png)
[OC-Foundation框架]---【集合数组】
随机推荐
[OC]-<UI入门>--常用控件-UIButton
七层网络体系结构
【文本生成】论文合集推荐丨 斯坦福研究者引入时间控制方法 长文本生成更流畅
Selenium+Pytest自动化测试框架实战(下)
使用标签模板解决用户恶意输入的问题
Redis之五大基础数据结构深入、应用场景
数字人主播618手语带货,便捷2780万名听障人士
LeetCode:41. 缺失的第一个正数
[oc]- < getting started with UI> -- learning common controls
Digital people anchor 618 sign language with goods, convenient for 27.8 million people with hearing impairment
postman之参数化详解
What is MySQL? What is the learning path of MySQL
Cesium draw points, lines, and faces
What is an R-value reference and what is the difference between it and an l-value?
Niuke winter vacation training 6 maze 2
LeetCode:394. 字符串解码
Leetcode: Sword Finger offer 42. Somme maximale des sous - tableaux consécutifs
LeetCode:836. 矩形重叠
Compétences en mémoire des graphiques UML
随手记01