Skip to content

Instantly share code, notes, and snippets.

@katychuang
Last active May 27, 2020 03:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save katychuang/7acc7ac28cb4aea6bd8381ce94330ec3 to your computer and use it in GitHub Desktop.
Save katychuang/7acc7ac28cb4aea6bd8381ce94330ec3 to your computer and use it in GitHub Desktop.
Awk script used for end of term reporting

This script and example were prepared to show how to use the awk program to split the BlackBoard download to multiple files. This assumes that you selected format of results = By Question and By User, such that each row represents a single question for a single student. See the csv file as an example.

Downloading from BlackBoard (separate row per question)

Go to Grade Center > Full Grade Center > Navigate to the Assignment or test's Column > Click on the down arrow next to the column's title > Select Download Results. Select 'By Question and User'. This gives you a file named Test.downloading.csv

Splitting into individual files

To run the awk program file you can try the following command.

awk -v field=a 'FS=",";
 FNR == 1 { hdr = $0 ; next }
  a != $1 { a = $1; close(name); name = $1"_"$3"_"$2".csv";  gsub(/"/,"",name); print hdr >> name }
          { print $0 >> name; } ' Test.downloadlong.csv

or, if you save the awk file below, you may run it with the following command where -f specifies the program file. Replace the final part with your csv input file.

awk -v field=a -f bb_downloadlong_split.awk Test.downloadlong.csv

This command is repeated below with comments for readability:

awk -v field=curr '
# This program splits the input csv file into multiple files, based on the value in the first column. 
# We assume that the first column is in sorted order so that the same value is consecutively arranged.

    FS=",";                                  # Define the delimiter in the input file
    
# Define rule for header
    FNR == 1 {hdr = $0 ; next}

# Define rule for when value changes
    curr != $1 { curr = $1;                  # save value of first column to variable a for comparison
              close(name);                   # close the last file before creating a new one
              name = $1"_"$3"_"$2".csv";     # create filename,i.e. 12345678_Mickey_Mouse.csv
              gsub(/"/,"",name);             # remove the double quotes from variable name
              print hdr > name}              # print the header to the new file
              
# Define rule for all rows              
            { print $0 > name; }             # print out the full row ($0) to the filename
   ' Test.downloadlong.csv

References

# To run the awk program file you can try the following command.
# awk -v field=a -f bb_downloadlong_split.awk Test.downloadlong.csv
# This program splits the input csv file into multiple files, based on the value in the first column.
# We assume that the first column is in sorted order so that the same value is consecutively arranged.
FS=","; # Define the delimiter in the input file
# Define rule for header
FNR == 1 {hdr = $0 ; next}
# Define rule for when value changes
curr != $1 { curr = $1; # save value of first column to variable a for comparison
close(name); # close the last file before creating a new one
name = $1"_"$3"_"$2".csv"; # create filename,i.e. 12345678_Mickey_Mouse.csv
gsub(/"/,"",name); # remove the double quotes from variable name
print hdr > name} # print the header to the new file
# Define rule for all rows
{ print $0 > name; } # print out the full row ($0) to the filename
Username Last Name First Name Question ID Question Answer Possible Points Auto Score Manual Score
12345678 Mouse Mickey Question ID 1 Mickey made his first appearance in what short film? Steamboat Willie 10 10
12345678 Mouse Mickey Question ID 2 What was originally a potential name for Mickey? Mortimer Mouse 10 10
12345677 Cow Clarabelle Question ID 1 Mickey made his first appearance in what short film? The Barn Dance 10 0
12345677 Cow Clarabelle Question ID 2 What was originally a potential name for Mickey? Felix the Cat 10 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment