1

I am writing output of 6 UNIX variables into a CSV file. Yes,now I have a CSV file with all the columns that I want.But, I want to use those variable names as column Name/ column Header.

How can we do this in bash ??

THis is what I have:

#!/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

Can somebody help me!!

2

Just add the header line before printing the data to the file:

#!/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

For the sake of completion, bash also has a variable name expansion feature, thus

${!Filename1@}

expands to the variable name

"Filename1"

and if $Filename1 and $Filename2 are set

${!Filename@}

expands to this space-separated variable name list:

"Filename1" "Filename2"

Unfortunately that’s not really helpful here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.