当前位置:网站首页>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 {
} \;
边栏推荐
- Redis之五大基础数据结构深入、应用场景
- What is the role of automated testing frameworks? Shanghai professional third-party software testing company Amway
- LeetCode41——First Missing Positive——hashing in place & swap
- LeetCode:39. 组合总和
- 【图的三大存储方式】只会用邻接矩阵就out了
- Intel Distiller工具包-量化实现3
- [oc]- < getting started with UI> -- common controls - prompt dialog box and wait for the prompt (circle)
- LeetCode:41. Missing first positive number
- LeetCode:236. 二叉树的最近公共祖先
- Cesium draw points, lines, and faces
猜你喜欢
The carousel component of ant design calls prev and next methods in TS (typescript) environment
Redis之性能指标、监控方式
MongoDB 的安装和基本操作
How to intercept the string correctly (for example, intercepting the stock in operation by applying the error information)
Chapter 1 :Application of Artificial intelligence in Drug Design:Opportunity and Challenges
CUDA实现focal_loss
LeetCode:498. Diagonal traversal
CUDA implementation of self defined convolution attention operator
LeetCode:124. Maximum path sum in binary tree
数字人主播618手语带货,便捷2780万名听障人士
随机推荐
Booking of tourism products in Gansu quadrupled: "green horse" became popular, and one room of B & B around Gansu museum was hard to find
What is the role of automated testing frameworks? Shanghai professional third-party software testing company Amway
五层网络体系结构
Li Kou daily question 1 (2)
AcWing 2456. 记事本
[oc]- < getting started with UI> -- common controls - prompt dialog box and wait for the prompt (circle)
BMINF的後訓練量化實現
Redis之Bitmap
什么是MySQL?MySql的学习之路是怎样的
Leetcode: Sword finger offer 48 The longest substring without repeated characters
注意力机制的一种卷积替代方式
Notes 01
Leetcode刷题题解2.1.1
LeetCode:劍指 Offer 42. 連續子數組的最大和
Nacos installation and service registration
[OC-Foundation框架]--<Copy对象复制>
数学建模2004B题(输电问题)
在QWidget上实现窗口阻塞
[oc]- < getting started with UI> -- learning common controls
Chapter 1 :Application of Artificial intelligence in Drug Design:Opportunity and Challenges