1

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.

enter image description here

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
1

2 Answers 2

5
pr -4T file
Scan Times:   Check Times:      Move Times:       Switch Times:
 33.3 seconds      110.30 seconds    73.66 seconds     92.0 seconds
 77.4 seconds      72.99 seconds     90.77 seconds     78.6 seconds
 33.3 seconds      72.16 seconds     72.87 seconds     77.8 seconds
 77.4 seconds      110.30 seconds    71.75 seconds     84.9 seconds

The empty line in each column will also be displayed.
Previously, you can make the necessary changes in the text, for example, using sed:

sed '/^\s*$/d;s/seconds//' file | pr -4T
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

Added:
It should be remembered that the utility was created for printing and has a number of default flags that are not entirely suitable for such cases.
-T eliminate pagination and omit page headers and trailers.
-wN Setting the page width. (default: -w 72).

Added2:
Or an example of how you can write each row to an awk array:

awk '
/^\s*$/ {i=0; next}
        {A[i]= A[++i] sprintf("%*s", 12,$($NF ~ /:$/?0:1))}
END     {for(i in A) print A[i]}
' file
 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
3
  • How do I add commas between the values using the pr -4T file command? Right now all 4 values are printed in a cell Commented Jul 17, 2021 at 22:39
  • pr -4T -i,10 it is possible to change the value 10 Commented Jul 18, 2021 at 10:54
  • Better to do it like this: sed '/^\s*$/d;s/\sseconds/,/' file | pr -4T Commented Jul 18, 2021 at 11:03
1

Using Raku (formerly known as Perl_6):

raku -e '.join("\t").put for [Z] lines.map(*.trim).batch(6);' 

OUTPUT:

Scan Times: Check Times:    Move Times: Switch Times:
33.3 seconds    110.30 seconds  73.66 seconds   92.0 seconds
77.4 seconds    72.99 seconds   90.77 seconds   78.6 seconds
33.3 seconds    72.16 seconds   72.87 seconds   77.8 seconds
77.4 seconds    110.30 seconds  71.75 seconds   84.9 seconds

Briefly, lines are trim-med (element-wise) and batch-ed (collectively) according to column length (6), producing 4 batches. Then the [Z] operator consecutively "zips" together one element from each batch. In a final formatting step, elements are join()-ed together with tabs to create a TSV file.

[Note: The batch operator will work on incomplete batches; if you only want complete batches, use rotor instead of batch. (However, rotor will fail silently on an incomplete final batch unless you set :partial)].

Remove " seconds" with a second Raku one-liner below, (...or add a subst() call to the first one-liner above):

perl6 -pe 's:g/" seconds"//;' 

https://raku.org/

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.