11

My output currently is:

echo "Time Taken to checkout svn repository repo_performance hosted on $host on `date` is : $Time_checkout seconds" >> log.csv
    echo "Time Taken to add $loopmax 10MB svn files in svn repository repo_performance  hosted on $host `date` is : $Time_add seconds" >> log.csv
    echo "Time Taken to commit $loopmax 10MB svn files in svn repository repo_performance  hosted on $host on `date` is : $Time_commit seconds" >> log.csv

But, I want to create a csv file which has Host, Date, Operation, Duration and its values in the rows.

How can I make it using scripting ?

5 Answers 5

12

You may just do:

echo "$host, `date`, checkout,$Time_checkout" >> log.csv
echo "$host, `date`, add, $Time_add" >> log.csv
echo "$host, `date`, cimmit, $Time_commits" >> log.csv
Sign up to request clarification or add additional context in comments.

1 Comment

Thats fine. but how do I create my csv file headers as Host, Date, Operation, Duration
7

you can try something like this;

printf "Host\tDate\tOperation\tDuration\n" >> log.csv
printf "$host\t$(date)\tTime Taken to checkout\t$Time_checkout\n" >> log.csv
printf "$host\t$(date)\tTime Taken to add $loopmax 10MB svn files\t$Time_add\n" >> log.csv
printf "$host\t$(date)\tTime Taken to commit $loopmax 10MB svn files\t$Time_commit\n" >> log.csv

1 Comment

printf seems better option instead of echo -e@Mustafa DOGRU
3

Just in case anyone is looking for CSV where values are in double quotes ("), you have to remove the outer quotes for the echo and use them (escaped) for each value instead, like this:

echo \"$host\",\"$date",\"checkout\",\"$checkout_time\" >> log.csv
echo \"$host\",\"$date",\"add\",\"$add_time\" >> log.csv
echo \"$host\",\"$date",\"commit\",\"$commit_time\" >> log.csv

I'd recommend writing a function to make your script less verbose and easier to read, like this.

write_csv(){
    echo \"$1\",\"$2\",\"$3\",\"$4\" >> log.csv
}
write_csv $host $date checkout $checkout_time
write_csv $host $date add $add_time
write_csv $host $date commit $commit_time

Comments

3

You can also generalize Mig82's answer to support variable arguments like this:

# Usage: csv <items>

csv()
{
    local items=("$@")

    (
    IFS=,
    echo "${items[*]}"
    )
}

csv 1 "2 3" >> log.csv

1 Comment

used this with a slight adjustment to handle quoting: ``` csv() { local items=("$@") # quote and escape as needed # datatracker.ietf.org/doc/html/rfc4180 for i in "${!items[@]}" do if [[ "${items[$i]}" =~ [,\"] ]] then items[$i]=\"$(echo -n "${items[$i]}" | sed s/\"/\"\"/g)\" fi done ( IFS=, echo "${items[*]}" ) } ```
1

used the answer from @Nishant with a slight adjustment to handle quoting:

    csv()
    {
        local items=("$@")
    
        # quote and escape as needed
        # https://datatracker.ietf.org/doc/html/rfc4180
        for i in "${!items[@]}"
        do
            if [[ "${items[$i]}" =~ [,\"] ]]
            then
                items[$i]=\"$(echo -n "${items[$i]}" | sed s/\"/\"\"/g)\"
            fi
        done
    
        (
        IFS=,
        echo "${items[*]}"
        )
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.