描述:
我有一段代码,通过匹配存在于多个格式的文件夹中的 .log 文件的关键词/句子来提取时间戳log_Job_*
。
它计算不同过程的时间持续时间并将输出写入 .CSV 文件。
我希望 .csv 中包含哪些列:
- 文件名
- 流程类型
- 进口
- 证实
- 。 。
- 主分区持续时间
现在的问题是,我在 2. 进程类型下有 2 种类型的进程。因此,想到在这里使用 case 语句。并执行以下操作:
#!/bin/bash
cd /path/to/manoj/version_2019_logs/
for file in log_Job_*/manoj.log; do
ProcessType1="$(grep -F 'Running process mpeXbrlImport.xml' "$file" | awk '{print $5}' | cut -c 4-)"
ProcessType2="$(grep -F 'Running process mpeXbrlValidate.xml' "$file" | awk '{print $5}' | cut -c 4-)"
ProcessType="$ProcessType1","$ProcessType2"
case $ProcessType in
$ProcessType1)
#set of commands to get other variaqbles
Var="$Filename","$ProcessType","$TotalDuration","$Initialization","$MPEProcessDuration","$TotalPartitionDuration","$WaitPartitionDuration","$MainPartionDuration"
echo $Var >>OutputFile_Import.csv
;;
*)
#repeat the set of commands and this time save with different variable names
Var1="$Filename1","$ProcessType1","$TotalDuration1","$Initialization1","$MPEProcessDuration1","$TotalPartitionDuration1","$WaitPartitionDuration1","$MainPartionDuration1"
echo $Var1 >>OutputFile_Validate.csv
;;
esac
done
我的计划是根据此创建两个单独的 .CSV 文件,Processtype
然后将两个文件连接起来。
问题:脚本正在成功执行,但最后只生成一文件即OutputFile_Validate.csv
我已经仔细检查了脚本,没有重复使用任何变量。有人能告诉我这是什么原因吗?
答案1
如果我理解你的问题,你想在ProcessType1
不为空时提供 import.csv,在ProcessType2
不为空时提供validate.csv。
test
如果变量已设置并且不为空,则可以使用以下命令:
if [ -n "$VAR" ];
then
echo "VAR is set\n"
fi
您的代码可能看起来像:
#!/bin/bash
cd /path/to/manoj/version_2019_logs/
for file in log_Job_*/manoj.log; do
ProcessType1="$(grep -F 'Running process mpeXbrlImport.xml' "$file" | awk '{print $5}' | cut -c 4-)"
ProcessType2="$(grep -F 'Running process mpeXbrlValidate.xml' "$file" | awk '{print $5}' | cut -c 4-)"
if [ -n "$ProcessType1" ];
then
#set of commands to get other variaqbles
Var="$Filename","$ProcessType1","$TotalDuration","$Initialization","$MPEProcessDuration","$TotalPartitionDuration","$WaitPartitionDuration","$MainPartionDuration"
echo $Var >>OutputFile_Import.csv
fi
if [ -n "$ProcessType2" ];
then
#repeat the set of commands and this time save with different variable names
Var1="$Filename1","$ProcessType2","$TotalDuration1","$Initialization1","$MPEProcessDuration1","$TotalPartitionDuration1","$WaitPartitionDuration1","$MainPartionDuration1"
echo $Var1 >>OutputFile_Validate.csv
fi
done