我有一个相当复杂的子任务:基于一个文件创建文件夹/目录(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#############
App.ActiveDocument.Spreadsheet.setAlias('B2', 'Rbig')
App.ActiveDocument.Spreadsheet.set('B2', '=5.0mm')#set Rbig here
App.ActiveDocument.Spreadsheet.setAlias('F2', 'Llower')
App.ActiveDocument.Spreadsheet.set('F2', '=9.0mm')#set Llower here
App.ActiveDocument.recompute()
预期输出:
对于PF.csv的每一行,将在其中创建目录和文件,并使用第一列DPx命名(x = 0,1,2,...)。此外,文件的内容来自FC.py,并使用PF.csv其他列(如下所述,方法)中的值更改某些行。
文件内容 DP0/DP0.py
###############Some description#############
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()
文件内容 DP1/DP1.py
###############Some description#############
App.ActiveDocument.Spreadsheet.setAlias('B2', 'Rbig')
App.ActiveDocument.Spreadsheet.set('B2', '=2.949mm')#set Rbig here
App.ActiveDocument.Spreadsheet.setAlias('F2', 'Llower')
App.ActiveDocument.Spreadsheet.set('F2', '=17.3mm')#set Llower here
App.ActiveDocument.recompute()
文件内容 DP2/DP2.py
###############Some description#############
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()
文件内容 DP3/DP3.py
###############Some description#############
App.ActiveDocument.Spreadsheet.setAlias('B2', 'Rbig')
App.ActiveDocument.Spreadsheet.set('B2', '=2.412mm')#set Rbig here
App.ActiveDocument.Spreadsheet.setAlias('F2', 'Llower')
App.ActiveDocument.Spreadsheet.set('F2', '=9.032mm')#set Llower here
App.ActiveDocument.recompute()
我的代码
awk 'BEGIN {FS = ",";}
{
if ($1 ~ "DP")
{Rbig = $2; Llower = $6; #values are assigned from each line read from PF.csv
gsub(" ",""); system("mkdir "$1); filename=$1"/"$1".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 ($0 ~ "#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 ($0 ~ "#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
我的代码输出(不需要)创建文件夹,但仅创建带有行DP2/DP2.py
的文件App.ActiveDocument.Spreadsheet.set('B2', '=3.103mm')#set Rbig here
。
我确信这是有可能的,但是由于缺乏理解而未能成功。请在您的答案中说明问题出在哪里以及如何使用AWK
解决该问题。在此先感谢您和圣诞快乐!
PS:我只能使用AWK接受答案,因为它是更大的一部分工作流程,但可以随意添加其他解决方案(如果可能的话)外壳脚本。另外,该脚本应使用Shell脚本调用,或直接在Linux / Mac的终端中键入。
所以这是我解决此问题的方法,我没有将检查目录及其创建部分与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]=$0
next
}
FNR>1{
sub(/ +/,"",$1)
file=$1"/"$1".py"
for(i=1;i<=count;i++){
num=split(a[i],array," ")
if(i==2){
sub(/[0-9]+\\.[0-9]+mm/,$6,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
我已经在我的TEST环境中成功测试了它,请不要直接在PROD中运行此代码,请先在非实时环境中对其进行测试。