我正在将 6 个 UNIX 变量的输出写入 CSV 文件。是的,现在我有一个包含我想要的所有列的 CSV 文件。但是,我想使用这些变量名作为列名/列标题。
我们怎样在 bash 中做到这一点?
这就是我所拥有的:
#!/bin/bash
cd /path/to/manoj/version_2019_logs/
for file in log_Job_*/manoj.log; do
set of commands
Var1="$Filename1","$ProcessType1","$TotalDuration1","$Initialization1","$MPEProcessDuration1","$TotalPartitionDuration1","$WaitPartitionDuration1","$MainPartionDuration1"
echo $Var1 >>OutputFile_Validate.csv
done
有人能帮帮我吗?
答案1
只需在将数据打印到文件之前添加标题行:
#!/bin/bash
Header="Filename1,ProcessType1,TotalDuration1,Initialization1,MPEProcessDuration1,TotalPartitionDuration1,WaitPartitionDuration1,MainPartionDuration1"
echo $Header >>OutputFile_Validate.csv
cd /path/to/manoj/version_2019_logs/
for file in log_Job_*/manoj.log; do
set of commands
Var1="$Filename1","$ProcessType1","$TotalDuration1","$Initialization1","$MPEProcessDuration1","$TotalPartitionDuration1","$WaitPartitionDuration1","$MainPartionDuration1"
echo $Var1 >>OutputFile_Validate.csv
done
为了完成,bash
还有一个变量名扩展特征,因此
${!Filename1@}
扩展为变量名
"Filename1"
并且如果$Filename1
和$Filename2
被设置
${!Filename@}
扩展为这个以空格分隔的变量名称列表:
"Filename1" "Filename2"
不幸的是,这在这里没有什么帮助。