[英]Bash script to mkdir on each line of a file that has been split by a delimiter?
Trying to figure out how to iterate through a .txt
file (filemappings.txt) line by line, then split each line using tab
( \\t
) as a delimiter so that we can create the directory specified on the right of the tab ( mkdir -p
). 试图弄清楚如何逐行遍历
.txt
文件(filemappings.txt),然后使用tab
( \\t
)作为分隔符拆分每一行,以便我们可以创建选项卡右侧指定的目录( mkdir -p
)。
Reading filemappings.txt and then splitting each line by tab
读取filemappings.txt,然后按
tab
拆分每一行
server/ /client/app/
server/a/ /client/app/a/
server/b/ /client/app/b/
Would turn into 会变成
mkdir -p /client/app/
mkdir -p /client/app/a/
mkdir -p /client/app/b/
Would xargs
be a good option? xargs
会是一个不错的选择吗? Why or why not? 为什么或者为什么不?
cut -f 2 filemappings.txt | tr '\n' '\0' | xargs -0 mkdir -p
xargs -0非常适合矢量操作。
You already have an answer telling you how to use xargs
. 您已经有一个答案告诉您如何使用
xargs
。 In my experience xargs
is useful when you want to run a simple command on a list of arguments that are easy to retrieve. 根据我的经验,当您想要在易于检索的参数列表上运行简单命令时,
xargs
非常有用。 In your example, xargs
will do nicelly. 在你的例子中,
xargs
会做得很好。 However, if you want to do something more complicated than run a simple command, you may want to use a while
loop: 但是,如果要执行比运行简单命令更复杂的操作,可能需要使用
while
循环:
while IFS=$'\t' read -r a b
do
mkdir -p "$b"
done <filemappings.txt
In this special case, read ab
will read two arguments separated by the defined IFS
and put each in a different variable. 在这种特殊情况下,
read ab
将读取由定义的IFS
分隔的两个参数,并将每个参数放在不同的变量中。 If you are a one-liner lover, you may also do: 如果你是单行情人,你也可以这样做:
while IFS=$'\t' read -r a b; do mkdir -p "$b"; done <filemappings.txt
In this way you may read multiple arguments to apply to any series of commands; 通过这种方式,您可以读取多个参数以应用于任何系列的命令; something that
xargs
is not well suited to do. xargs
不太适合做的事情。
Using read -r
will read a line literally regardless of any backslashes in it, in case you need to read a line with backslashes. 使用
read -r
将逐字读取一行,而不管其中是否有任何反斜杠,以防您需要读取带反斜杠的行。
Also note that some operating systems may allow tabs as part of a file or directory name. 另请注意,某些操作系统可能允许选项卡作为文件或目录名称的一部分。 That would break the use of the tab as the separator of arguments.
这将打破使用选项卡作为参数的分隔符。
sed -n '/\t/{s:^.*\t\t*:mkdir -p ":;s:$:":;p}' filemappings.txt | bash
sed -n
: only work with lines that contains tab
(delimiter) sed -n
:仅适用于包含tab
符(分隔符)的行 s:^.*\\t\\t*:mkdir -p :
: change all things from line beggning to tab
to mkdir -p
s:^.*\\t\\t*:mkdir -p :
::将所有内容从行beggning更改为tab
到mkdir -p
| bash
| bash
: tell bash
to create folders | bash
:告诉bash
创建文件夹 As others have pointed out, \\t
character could also be a part of the file or directory name, and the following command may fail. 正如其他人指出的那样,
\\t
字符也可以是文件或目录名的一部分,以下命令可能会失败。 Assuming the question represents the true form of the input file, one can use: 假设问题代表输入文件的真实形式,可以使用:
$ grep -o -P '(?<=\t).*' filemappings.txt | xargs -d'\n' mkdir -p
It uses -P
perl-style regex to get words after the \\t
(TAB) character, then use -d'\\n'
which provides all relevant lines as a single input to mkdir -p
. 它使用
-P
perl-style regex来获取\\t
(TAB)字符后的单词,然后使用-d'\\n'
,它将所有相关行作为mkdir -p
的单个输入。
使用GNU Parallel,它看起来像这样:
parallel --colsep '\t' mkdir -p {2} < filemapping.txt
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.