简体   繁体   English

从列表中创建保留空格的目录

[英]Creating directories from list preserving whitespaces

I have list of names in a file that I need to create directories from. 我有一个文件中的名称列表,我需要从中创建目录。 The list looks like 该列表看起来像

Ada Lovelace
Jean Bartik
Leah Culver

I need the folders to be the exact same, preserving the whitespace(s). 我需要文件夹完全相同,保留空格。 But with 但随着

awk '{print $0}' myfile | xargs mkdir

I create separate folders for each word 我为每个单词创建单独的文件夹

Ada
Lovelace
Jean
Bartik
Leah
Culver

Same happens with 同样如此

 awk '{print $1 " " $2}' myfile | xargs mkdir

Where is the error? 错误在哪里?

Using gnu xargs you can use -d option to set delimiter as \\n only. 使用gnu xargs您可以使用-d选项将分隔符设置为\\n only。 This way you can avoid awk also. 这样你也可以避免使用awk

xargs -d '\n' mkdir -p < file

If you don't have gnu xargs then you can use tr to convert all \\n to \\0 first: 如果你没有gnu xargs那么你可以使用tr将所有\\n首先转换为\\0

tr '\n' '\0' < file | xargs -0 mkdir

@birgit:尝试:完全基于您提供的示例Input_file。

awk -vs1="\"" 'BEGIN{printf "mkdir  ";}{printf("%s%s%s ",s1,$0,s1);} END{print ""}'   Input_file | sh
awk '{ system ( sprintf( "mkdir \"%s\"", $0)) }' YourFile
# OR
awk '{ print"mkdir "\"" $0 "\"" | "/bin/sh" }' YourFile
# OR for 1 subshell
awk '{ Cmd = sprintf( "%s%smkdir \"%s\"", Cmd, (NR==1?"":"\n"), $0) } END { system ( Cmd ) }' YourFile 
  • Last version is better due to creation of only 1 subshell. 由于仅创建了1个子shell,因此最新版本更好。
  • If there are a huge amount of folder (shell parameter limitation), you could loop and create smaller command several times 如果有大量的文件夹(shell参数限制),您可以循环并多次创建较小的命令

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM