Issue
I have a .txt file with the following data structure:
Scan Times:
33.3 seconds
77.4 seconds
33.3 seconds
77.4 seconds
Check Times:
110.30 seconds
72.99 seconds
72.16 seconds
110.30 seconds
Move Times:
73.66 seconds
90.77 seconds
72.87 seconds
71.75 seconds
Switch Times:
92.0 seconds
78.6 seconds
77.8 seconds
84.9 seconds
I now want to take that .txt file and create a CSV file that has the following format.
I have a very basic layout so far with my bash script but I am not sure how to proceed:
inputFiles=("./Successes/SuccessSummary.txt" "./Failures/FailSummary.txt")
touch results.csv
for file in "${inputFiles[@]}"
do
while IFS= read -r line
do
#echo $line
if [ "$line" = "Scan Times:" ]
then
fi
if [ "$line" = "Check Times:" ]
then
fi
if [ "$line" = "Move Times:" ]
then
fi
if [ "$line" = "Switch Distances:" ]
then
fi
done < "$file"
done
Solution
Here's an awk
script that does it:
#!/usr/bin/awk -f
BEGIN {
OFS=","
colnum=0
}
/:$/ {
data[++colnum,1]=$0
rownum=1
}
/seconds$/ {
data[colnum,++rownum]=$1
}
END {
for (r = 1; r <= rownum; r++) {
for (c = 1; c <= colnum; c++) {
printf "%s%s", data[c,r], (c == colnum ? RS : OFS)
}
}
}
Example:
$ ./pivot input.txt
Scan Times:,Check Times:,Move Times:,Switch Times:
33.3,110.30,73.66,92.0
77.4,72.99,90.77,78.6
33.3,72.16,72.87,77.8
77.4,110.30,71.75,84.9
Answered By - Shawn