假设我有一个类似的文本文件:
john
george
james
stewert
每个名称都放在单独的行中。
我想阅读此文本文件的各行,并为每个名称创建一个文本文件,例如:john.txt
,george.txt
等等。
我如何在Bash中做到这一点?
touch
:while read line; do touch "$line.txt"; done <in
while read line; [...]; done <in
:这会read
一直运行直到read
自身返回1
,这会在到达文件末尾时发生;输入read
是从in
当前工作目录中命名的文件而不是由于<in
重定向而从终端读取的;touch "$line.txt"
:这touch
基于的扩展值$line.txt
,line
其后是.txt
;的内容;touch
如果不存在则创建文件,如果存在则更新访问时间;xargs
+ touch
:xargs -a in -I name touch name.txt
-a in
:xargs
从in
当前工作目录中命名的文件中读取其输入;-I name
:在以下命令中,用输入的当前行xargs
替换每次name
出现的;touch name
:touch
在的替换值上运行name
; 如果不存在,它将创建文件,如果存在,它将更新其访问时间;% ls
in
% cat in
john
george
james
stewert
% while read line; do touch "$line.txt"; done <in
% ls
george.txt in james.txt john.txt stewert.txt
% rm *.txt
% xargs -a in -I name touch name.txt
% ls
george.txt in james.txt john.txt stewert.txt
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句