This script is intended to
  • pull the data group number from the CSV file title
  • indentify and pull the group index ("Index"), event number ("Event") and mass ("M" or "Mt") from the file contents
  • construct the Masterclass.Events primary key o_no
  • write these to CIMA-master.csv in a format mirroring that of the Events table in preparation for import into it.

# Put all csv files to be evaluated in one directory, with no other files.
# Run this from the parent directory, with the csv directory as an argument
# i.e.,
#   ~/cima/
#   ~/cima/csv-files/(csv files)
# Running
#   username:~/cima$ ./ csv-files/ 
# will produce /cima/CIMA-master.csv

# The size of each data group (assumed uniform):

# clear the output file:
> $thisdir/CIMA-master.csv
# Write the column header line
echo "o_no,g_no,g_index,ev_no,mass" >> $thisdir/CIMA-master.csv

# Run through all of the .csv files in the argument directory
for csv in $(ls $thisdir/$1); do
      # TODO: check for .csv extension, break if not there
      # Find the group number from the filename
      # Index of the dash immediately following group number
      #   (should be 14, 15, or 16)
      dash=`expr index "$csv" -`
      # group number starts at index 12

      # Import first row as $(#head -n 1 $1/$csv)
      # Turn that csv list of column headers into array $columns[]
      IFS=, read -r -a columns <<< "$(head -n 1 $1/$csv)"

      # Find where Event, Mass and Index values are in $columns[]
      # ${!columns[*]} is an array of all indices in $columns;
      # i.e. 0,1,2,...
      for i in ${!columns[*]}; do
            if [ ${columns[$i]} == Event ]; then
            if [ ${columns[$i]} == M ]; then
            elif [ ${columns[$i]} == Mt ]; then
            if [ ${columns[$i]} == Index ]; then

      # Read the rest of the file line-by-line
      while IFS= read -r -a dataline
            # Break the line into array $values[]
            IFS=, read -r -a values <<< "$dataline"
            oNo=$(( (($groupNo-1)*$groupSize)+${values[$iIndex]} ))
            # Print those values relevant to the Events table of the
            #   CIMA Masterclass database
            # o_no (primary key), g_no, g_index, event_no, mass
            echo "$oNo,$groupNo,${values[$iIndex]},${values[$iEvent]},${values[$iMass]}" >> $thisdir/CIMA-master.csv
      done < <(tail -n +2 $1/$csv)
      # (tail -n +2 returns everything but the first line)


-- Main.JoelG - 2017-03-03


Topic revision: r1 - 2017-03-03, JoelG
This site is powered by FoswikiCopyright © by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding Foswiki? Send feedback