我正在尝试使用awk
在目录中创建子目录(该目录始终是file1
的最后一行,每个块用空行分隔),如果该行中的数字在file2
的{{1}}中发现$2
的2个(总是以xx-xxxx格式的前6位数字)。
该目录已经在file1
中创建。在下面的示例中,/path/to/directory
中已经存在Directory2_2
,并且由于{{1}的/path/to/directory
中有19-0003
,19-0004
和19-0005
},它们被移到$2
。
文件1
file1
文件2
Directory2_2
在bash中寻求循环
xxxx_006 19-0000_Lname-yyyy-zzzzz
xxxx_007 19-0001_Lname-yyyy-zzzzz
Directory1_1
xxxx_008 19-0003_Lname-yyyy-zzzzz
xxxx_009 19-0004_Lname-yyyy-zzzzz
xxxx_020 19-0005_Lname-yyyy-zzzzz
Directory2_2
所需的输出
xxxx
19-0003-xxx-xxx-xxx_000-111
yyyy
xxxx
19-0004-xxx-xxx-xxx_000-111
yyyy
xxxx
19-0005-xxx-xxx-xxx_000-111
yyyy
答案 0 :(得分:1)
请您尝试以下。
awk -v path_val="/your_path/" '
FNR==NR{
if($0 ~ /^[0-9]+/){
a[substr($0,1,7)]=$0
}
next
}
/^Directory/{
if(count==value){
print "Directory " $0 " all elements are present." ORS "Going to write shell script code now..."
print $0 ORS val
print "*************************************************"
print "if [[ -d " path_val $0 " ]]" ORS "then" ORS\
" cd " path_val $0 ORS " mkdir " val ORS\
" if [[ $? -eq 0 ]]" ORS " then" ORS \
" echo " s1 "Directories named "\
val s1 " created successfully in path " path_val\
"." s1 ORS " else" ORS " echo " s1\
"kindly check from your end once seems directories not created." s1\
ORS " fi" ORS "else" ORS " echo " s1\
"Please check seems base directory " path_val " NOT present itself."\
s1 ORS "fi"
}
count=val=value=""
}
($3 in a){
val=(val?val OFS a[$3]:a[$3])
count++
}
/^xxx/{
value++
}' Input_file2 FS="[ _]" Input_file1
说明代码的作用是:
1-代码中有一个名为/your_path/
的变量,它是创建目录等的BASE路径。
2-它将检查Input_file2中Directory_...
(para_para)关键字之前的所有行是否都存在于Input_file2中,然后它将输出这些行的输出以及目录名+它将写入控制台上的代码(bash代码,它检查您的基本目录路径,然后在基本目录中创建匹配的目录)。到目前为止,我只是简单地打印它,您可以将其带到.ksh
文件(作为输出文件)并可以运行它,也可以在此代码末尾添加| bash
。我还没有测试,我留给OP。
以下将是输出:
Directory Directory2_2 all elements are present.
Going to write shell script code now...
Directory2_2
19-0003-xxx-xxx-xxx_000-111 19-0004-xxx-xxx-xxx_000-111 19-0005-xxx-xxx-xxx_000-111
*************************************************
if [[ -d /your_path/Directory2_2 ]]
then
cd /your_path/Directory2_2
mkdir 19-0003-xxx-xxx-xxx_000-111 19-0004-xxx-xxx-xxx_000-111 19-0005-xxx-xxx-xxx_000-111
if [[ $? -eq 0 ]]
then
echo Directories named 19-0003-xxx-xxx-xxx_000-111 19-0004-xxx-xxx-xxx_000-111 19-0005-xxx-xxx-xxx_000-111 created successfully in path /your_path/.
else
echo kindly check from your end once seems directories not created.
fi
else
echo Please check seems base directory /your_path/ NOT present itself.
fi
PS: 如上所述,请获取最终的shell代码,该代码应在系统的output_file中创建目录,或者在{的末尾使用| bash
等运行它{1}}代码我还没有测试过。未经测试,请勿运行代码。语句非常简单,您可以遍历它们,并且只应在测试目录/测试环境中对其进行测试。
答案 1 :(得分:1)
如果目录名中不包含空格(在以段模式处理的file1和以行模式处理的file2下面):
awk 'NR==FNR { for(i=2; i<NF; i+=2) a[substr($i,1,7)] = $NF; next }
{ k = substr($0, 1, 7) }
k in a { cmd = sprintf("mkdir -p %s/%s", a[k], $0); print(cmd); }
' RS= file1 RS='\n' file2
#mkdir -p Directory2_2/19-0003-xxx-xxx-xxx_000-111
#mkdir -p Directory2_2/19-0004-xxx-xxx-xxx_000-111
#mkdir -p Directory2_2/19-0005-xxx-xxx-xxx_000-111
将 print(cmd)更改为 system(cmd)以实际运行命令。
注意:如果目录名称包含空格,则可能需要设置IFS ='\ n'才能将$ NF用于file1中的基本目录:
awk 'NR==FNR { for(i=1; i<NF; i++) a[substr($i,index($i," ")+1,7)] = $NF; next }
{ k = substr($0, 1, 7) }
k in a { cmd = sprintf("mkdir -p \"%s\"/\"%s\"", a[k], $0); print(cmd); }
' FS='\n' RS= file1 RS='\n' file2