使用 AWK 基于一个文件创建文件夹,并根据另一个文件创建这些文件夹中的文件
Creating folder based on one file and files in those folders based on another file using AWK
我有一个相当复杂的子任务:
基于一个文件创建 folders/directories (PF.csv
) 并基于另一个文件创建文件 (FC.py
) 在那些目录中。
要使用的两个输入文件
文件内容 PF.csv
#######Some description#######
,Rbig,Rsmall,Rmiddle,Lupper,Llower,
DP 0,4.590,0.424,3.5,20,20,,,,,
DP 1,2.949,0.192,1.831,8.508,17.3,,,,,
DP 2,3.103,0.812,1.662,11.456,7.666,,,,,
DP 3,2.418,0.058,1.876,6.295,9.032,,,,,
文件内容 FC.py
###############Some description#############
Lorem ipsum
App.ActiveDocument.Spreadsheet.set('B2', '=5.0mm')#set Rbig here
Lorem ipsum
App.ActiveDocument.Spreadsheet.set('F2', '=9.0mm')#set Llower here
预期输出:
对于 PF.csv 的每一行,将创建一个目录和其中的文件,使用第一列 DPx (x = 0,1,2,...) 命名。此外,文件的内容来自 FC.py,使用 PF.csv.
的其他列(如下所述,如何)的值更改某些行
文件内容 DP0/DP0.py
###############Some description#############
Lorem ipsum
App.ActiveDocument.Spreadsheet.set('B2', '=4.590mm')#set Rbig here
Lorem ipsum
App.ActiveDocument.Spreadsheet.set('F2', '=20mm')#set Llower here
文件内容 DP1/DP1.py
###############Some description#############
Lorem ipsum
App.ActiveDocument.Spreadsheet.set('B2', '=2.949mm')#set Rbig here
Lorem ipsum
App.ActiveDocument.Spreadsheet.set('F2', '=17.3mm')#set Llower here
文件内容 DP2/DP2.py
###############Some description#############
Lorem ipsum
App.ActiveDocument.Spreadsheet.set('B2', '=3.103mm')#set Rbig here
Lorem ipsum
App.ActiveDocument.Spreadsheet.set('F2', '=7.666mm')#set Llower here
文件内容 DP3/DP3.py
###############Some description#############
Lorem ipsum
App.ActiveDocument.Spreadsheet.set('B2', '=2.412mm')#set Rbig here
Lorem ipsum
App.ActiveDocument.Spreadsheet.set('F2', '=9.032mm')#set Llower here
我的代码
awk 'BEGIN {FS = ",";}
{
if ( ~ "DP")
{Rbig = ; Llower = ; #values are assigned from each line read from PF.csv
gsub(" ",""); system("mkdir "); filename="/"".txt"; #empty space is pruned from first column ('DP x', x=0,1,2,...) and folder with file is created with the name using system() and filename
{(getline < "FC.py"); #FC.py is read, processing further FC.py only, taking the folders and values assigned using the previous codes for file PF.csv only.
{
if ([=16=] ~ "#set Rbig here") #if it finds a line with this, it assigns the value of Rbig, taken from PF.csv just before.
{gsub("5.0mm",Rbig"mm"); print >> filename;}
else if ([=16=] ~ "#set Llower here") #simlarly assigns Llower like previous two line codes
{gsub("9.0mm",Ll"mm"); print >> filename;}
else
{print >> filename;}
}; close(filename)
}
}
}
' PF.csv
我的代码输出(不需要)
文件夹已创建,但仅创建了包含行 App.ActiveDocument.Spreadsheet.set('B2', '=3.103mm')#set Rbig here
的文件 DP2/DP2.py
。
我相信这是可能的,但由于缺乏理解而没有成功。请在您的回答中说明问题出在哪里以及您是如何使用 AWK
.
克服它的
PS: I can only accept answers using only AWK as it's part of a bigger
workflow, but feel free to add other solutions, if possible using
shell scripts. Also, the script should be called using a shell script or typed directly in terminal in Linux/Mac.
EDIT2: 正如在聊天室中与 OP 讨论的那样,控制 M 字符也出现在 OP 的 Input_file(s ) 所以可以通过以下方式删除它们:
tr -d '\r' < Input_file > temp && mv temp Input_file
或者如果你的盒子里有 dos2unix
实用程序,你也可以 运行 删除控制 M 字符,一旦它们被删除,我的代码将 运行 适合你。
所以这是我解决这个问题的方法,我没有将检查目录及其创建部分与 awk
合并。
cat script.ksh
##First part is doing directory verification here.
while IFS=, read field1 field2 field3 field4 field5 rest
do
value="${field1/ /}"
if [[ -n "$value" ]]
then
if [[ ! -d "$value" ]]
then
mkdir "$value"
else
echo "Directory named $value is already existed."
fi
else
echo "first field is empty so, skipping this line..."
fi
done < "PF.csv"
##This part is responsible for file creation.
awk '
FNR==NR{
a[++count]=[=11=]
next
}
FNR>1{
sub(/ +/,"",)
file="/"".py"
for(i=1;i<=count;i++){
num=split(a[i],array," ")
if(i==2 || i==4){
val_sub=i==2?:
sub(/[0-9]+\.[0-9]+mm/,val_sub"mm",array[2])
for(k=1;k<=num;k++){
val=(val?val OFS:"")array[k]
}
print val > (file)
val=""
}
else{
print a[i] > (file)
}
}
print a[i] > (file)
close(file)
file=val=""
delete array
}' FC.py FS="," PF.csv
我已在我的测试环境中成功测试它,请不要运行直接在生产环境中使用此代码,请先在非实时环境中对其进行测试。
代码执行后成功创建文件:
cat DP2/DP2.py
App.ActiveDocument.Spreadsheet.setAlias('B2', 'Rbig')
App.ActiveDocument.Spreadsheet.set('B2', '=3.103mm')#set Rbig here
App.ActiveDocument.Spreadsheet.setAlias('F2', 'Llower')
App.ActiveDocument.Spreadsheet.set('F2', '=7.666mm')#set Llower here
App.ActiveDocument.recompute()
cat DP0/DP0.py
App.ActiveDocument.Spreadsheet.setAlias('B2', 'Rbig')
App.ActiveDocument.Spreadsheet.set('B2', '=4.590mm')#set Rbig here
App.ActiveDocument.Spreadsheet.setAlias('F2', 'Llower')
App.ActiveDocument.Spreadsheet.set('F2', '=20mm')#set Llower here
App.ActiveDocument.recompute()
我有一个相当复杂的子任务:
基于一个文件创建 folders/directories (PF.csv
) 并基于另一个文件创建文件 (FC.py
) 在那些目录中。
要使用的两个输入文件
文件内容 PF.csv
#######Some description#######
,Rbig,Rsmall,Rmiddle,Lupper,Llower,
DP 0,4.590,0.424,3.5,20,20,,,,,
DP 1,2.949,0.192,1.831,8.508,17.3,,,,,
DP 2,3.103,0.812,1.662,11.456,7.666,,,,,
DP 3,2.418,0.058,1.876,6.295,9.032,,,,,
文件内容 FC.py
###############Some description#############
Lorem ipsum
App.ActiveDocument.Spreadsheet.set('B2', '=5.0mm')#set Rbig here
Lorem ipsum
App.ActiveDocument.Spreadsheet.set('F2', '=9.0mm')#set Llower here
预期输出:
文件内容 DP0/DP0.py
###############Some description#############
Lorem ipsum
App.ActiveDocument.Spreadsheet.set('B2', '=4.590mm')#set Rbig here
Lorem ipsum
App.ActiveDocument.Spreadsheet.set('F2', '=20mm')#set Llower here
文件内容 DP1/DP1.py
###############Some description#############
Lorem ipsum
App.ActiveDocument.Spreadsheet.set('B2', '=2.949mm')#set Rbig here
Lorem ipsum
App.ActiveDocument.Spreadsheet.set('F2', '=17.3mm')#set Llower here
文件内容 DP2/DP2.py
###############Some description#############
Lorem ipsum
App.ActiveDocument.Spreadsheet.set('B2', '=3.103mm')#set Rbig here
Lorem ipsum
App.ActiveDocument.Spreadsheet.set('F2', '=7.666mm')#set Llower here
文件内容 DP3/DP3.py
###############Some description#############
Lorem ipsum
App.ActiveDocument.Spreadsheet.set('B2', '=2.412mm')#set Rbig here
Lorem ipsum
App.ActiveDocument.Spreadsheet.set('F2', '=9.032mm')#set Llower here
我的代码
awk 'BEGIN {FS = ",";}
{
if ( ~ "DP")
{Rbig = ; Llower = ; #values are assigned from each line read from PF.csv
gsub(" ",""); system("mkdir "); filename="/"".txt"; #empty space is pruned from first column ('DP x', x=0,1,2,...) and folder with file is created with the name using system() and filename
{(getline < "FC.py"); #FC.py is read, processing further FC.py only, taking the folders and values assigned using the previous codes for file PF.csv only.
{
if ([=16=] ~ "#set Rbig here") #if it finds a line with this, it assigns the value of Rbig, taken from PF.csv just before.
{gsub("5.0mm",Rbig"mm"); print >> filename;}
else if ([=16=] ~ "#set Llower here") #simlarly assigns Llower like previous two line codes
{gsub("9.0mm",Ll"mm"); print >> filename;}
else
{print >> filename;}
}; close(filename)
}
}
}
' PF.csv
我的代码输出(不需要)
文件夹已创建,但仅创建了包含行 App.ActiveDocument.Spreadsheet.set('B2', '=3.103mm')#set Rbig here
的文件 DP2/DP2.py
。
我相信这是可能的,但由于缺乏理解而没有成功。请在您的回答中说明问题出在哪里以及您是如何使用 AWK
.
PS: I can only accept answers using only AWK as it's part of a bigger workflow, but feel free to add other solutions, if possible using shell scripts. Also, the script should be called using a shell script or typed directly in terminal in Linux/Mac.
EDIT2: 正如在聊天室中与 OP 讨论的那样,控制 M 字符也出现在 OP 的 Input_file(s ) 所以可以通过以下方式删除它们:
tr -d '\r' < Input_file > temp && mv temp Input_file
或者如果你的盒子里有 dos2unix
实用程序,你也可以 运行 删除控制 M 字符,一旦它们被删除,我的代码将 运行 适合你。
所以这是我解决这个问题的方法,我没有将检查目录及其创建部分与 awk
合并。
cat script.ksh
##First part is doing directory verification here.
while IFS=, read field1 field2 field3 field4 field5 rest
do
value="${field1/ /}"
if [[ -n "$value" ]]
then
if [[ ! -d "$value" ]]
then
mkdir "$value"
else
echo "Directory named $value is already existed."
fi
else
echo "first field is empty so, skipping this line..."
fi
done < "PF.csv"
##This part is responsible for file creation.
awk '
FNR==NR{
a[++count]=[=11=]
next
}
FNR>1{
sub(/ +/,"",)
file="/"".py"
for(i=1;i<=count;i++){
num=split(a[i],array," ")
if(i==2 || i==4){
val_sub=i==2?:
sub(/[0-9]+\.[0-9]+mm/,val_sub"mm",array[2])
for(k=1;k<=num;k++){
val=(val?val OFS:"")array[k]
}
print val > (file)
val=""
}
else{
print a[i] > (file)
}
}
print a[i] > (file)
close(file)
file=val=""
delete array
}' FC.py FS="," PF.csv
我已在我的测试环境中成功测试它,请不要运行直接在生产环境中使用此代码,请先在非实时环境中对其进行测试。
代码执行后成功创建文件:
cat DP2/DP2.py
App.ActiveDocument.Spreadsheet.setAlias('B2', 'Rbig')
App.ActiveDocument.Spreadsheet.set('B2', '=3.103mm')#set Rbig here
App.ActiveDocument.Spreadsheet.setAlias('F2', 'Llower')
App.ActiveDocument.Spreadsheet.set('F2', '=7.666mm')#set Llower here
App.ActiveDocument.recompute()
cat DP0/DP0.py
App.ActiveDocument.Spreadsheet.setAlias('B2', 'Rbig')
App.ActiveDocument.Spreadsheet.set('B2', '=4.590mm')#set Rbig here
App.ActiveDocument.Spreadsheet.setAlias('F2', 'Llower')
App.ActiveDocument.Spreadsheet.set('F2', '=20mm')#set Llower here
App.ActiveDocument.recompute()