前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Shell·xargs命令用法

Shell·xargs命令用法

作者头像
netkiller old
发布2018-03-05 18:07:03
9890
发布2018-03-05 18:07:03
举报
文章被收录于专栏:NetkillerNetkiller

本文节选自《Netkiller Shell 手札》

3.12. standard input/output

3.12.1. xargs - build and execute command lines from standard input

xargs命令用法

3.12.1.1. 格式化

xargs用作替换工具,读取输入数据重新格式化后输出。

代码语言:javascript
复制
			定义一个测试文件,内有多行文本数据:

cat >> test.txt <<EOF

a b c d e f g
h i j k l m n
o p q
r s t
u v w x y z

EOF

# cat test.txt 

a b c d e f g
h i j k l m n
o p q
r s t
u v w x y z


多行输入一行输出:

# cat test.txt | xargs
a b c d e f g h i j k l m n o p q r s t u v w x y z

等效
# cat test.txt | tr "\n" " "
a b c d e f g h i j k l m n o p q r s t u v w x y z

-n选项多行输出:

# cat test.txt | xargs -n3
a b c
d e f
g h i
j k l
m n o
p q r
s t u
v w x
y z
# cat test.txt | xargs -n4
a b c d
e f g h
i j k l
m n o p
q r s t
u v w x
y z
# cat test.txt | xargs -n5
a b c d e
f g h i j
k l m n o
p q r s t
u v w x y
z


-d选项可以自定义一个定界符:

# echo "name|age|sex|birthday" | xargs -d"|"
name age sex birthday

结合-n选项使用:

# echo "name=Neo|age=30|sex=T|birthday=1980" | xargs -d"|" -n1
name=Neo
age=30
sex=T
birthday=1980			
3.12.1.2. standard input
代码语言:javascript
复制
			# xargs < test.txt 
a b c d e f g h i j k l m n o p q r s t u v w x y z		
		
# cat /etc/passwd | cut -d : -f1 > users
# xargs -n1 < users echo "Your name is"
Your name is root
Your name is bin
Your name is daemon
Your name is adm
Your name is lp
Your name is sync
Your name is shutdown
Your name is halt
Your name is mail
Your name is operator
Your name is games
Your name is ftp
Your name is nobody
Your name is dbus
Your name is polkitd
Your name is avahi
Your name is avahi-autoipd
Your name is postfix
Your name is sshd
Your name is neo
Your name is ntp
Your name is opendkim
Your name is netkiller
Your name is tcpdump		
3.12.1.3. -I 替换操作
代码语言:javascript
复制
复制所有图片文件到 /data/images 目录下:

ls *.jpg | xargs -n1 -I cp {} /data/images			
代码语言:javascript
复制
			读取stdin,将格式化后的参数传递给命令xargs的一个选项-I,使用-I指定一个替换字符串{},这个字符串在xargs扩展时会被替换掉,当-I与xargs结合使用,每一个参数命令都会被执行一次:

# echo "name=Neo|age=30|sex=T|birthday=1980" | xargs -d"|" -n1 | xargs -I {} echo "select * from tab where {} "
select * from tab where name=Neo 
select * from tab where age=30 
select * from tab where sex=T 
select * from tab where birthday=1980 

# xargs -I user echo "Hello user" <users 
Hello root
Hello bin
Hello daemon
Hello adm
Hello lp
Hello sync
Hello shutdown
Hello halt
Hello mail
Hello operator
Hello games
Hello ftp
Hello nobody
Hello dbus
Hello polkitd
Hello avahi
Hello avahi-autoipd
Hello postfix
Hello sshd
Hello netkiller
Hello neo
Hello tss
Hello ntp
Hello opendkim
Hello noreply
Hello tcpdump
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2016-11-03,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Netkiller 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 3.12. standard input/output
    • 3.12.1. xargs - build and execute command lines from standard input
      • 3.12.1.1. 格式化
      • 3.12.1.2. standard input
      • 3.12.1.3. -I 替换操作
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档