Organizing the output of my shell script into tables within the text file Organizing the output of my shell script into tables within the text file unix unix

Organizing the output of my shell script into tables within the text file


#!/bin/shin_file=in.txt      # Input fileparams=3            # Parameters countres_file=$(mktemp)  # Temporary filesep=' '             # Separator character# Print headercnt=0for i in $(cat $in_file | head -$((params*2))); do    if [ $((cnt % 2)) -eq 0 ]; then        echo $i    fi    cnt=$((cnt+1))done | sed ":a;N;\$!ba;s/\n/$sep/g" >>$res_file# Parse and print valuescnt=0for i in $(cat $in_file); do    # Print values, skip param names    if [ $((cnt % 2)) -eq 1 ]; then        echo -n $i >>$res_file    fi    if [ $(((cnt+1) % (params*2))) -eq 0 ]; then        # Values line is finished, print newline        echo >>$res_file    elif [ $((cnt % 2)) -eq 1 ]; then        # More values expected to be printed on this line        echo -n "$sep" >>$res_file    fi    cnt=$((cnt+1))done# Make nice table formatcat $res_file | column -trm -f $res_file

Explanation

This scripts assumes that:

  • input file is called "in.txt" (see in_file variable)
  • input file uses format you described in question
  • result table should have 3 columns (see params variable)

Most of the code is just parsing of your input data format. Actual column formatting is done by column tool.

If you want to export this table to excel, just change sep variable to ',' and save result output to .csv file. This file can be easily imported in excel application.

Example

Input file:

Size387NameVelvetTime13Size31415NameMiniaTime18Size31337NameABCDEFTime42

Script output:

Size   Name    Time387    Velvet  1331415  Minia   1831337  ABCDEF  42


Sam's answer provided exactly what you are looking for, but you can also consider making it more streamlined, avoiding the need to convert the metrics file into a table, and just write the table right away. For example, write a single script like this, user_input.bash:

echo "Enter the size (Mb or Gb) of your data set:" > /dev/stderrread SIZEOFDATASETecho "The size of your data set is $SIZEOFDATASET" > /dev/stderrecho "Enter the name of your assembler" > /dev/stderrread NAMEOFASSEMBLERecho "You are using $NAMEOFASSEMBLER as your assembler" > /dev/stderrecho "Enter Time:" > /dev/stderrread TIMEecho "You entered Time:" $TIME > /dev/stderrecho "Name Size Time"echo $NAMEOFASSEMBLER $SIZEOFDATASET $TIME

To use program:

 ./user_input.bash > metrics.file.1.txt    ./user_input.bash > metrics.file.2.txt    ./user_input.bash > metrics.file.3.txt    ...

The collect all results:

head -n 1  metrics.file.1.txt > allmetrics.txttail -n +2 -q metrics.file.*.txt > allmetrics.txt

HTH