Skip to content

Instantly share code, notes, and snippets.

@artyom-poptsov
Created October 30, 2015 08:33
Show Gist options
  • Save artyom-poptsov/70b4cdb4747f427ed299 to your computer and use it in GitHub Desktop.
Save artyom-poptsov/70b4cdb4747f427ed299 to your computer and use it in GitHub Desktop.
A Bash script that summarises up the data from a DSV file.
#!/bin/bash
### summary.sh -- Summarize the data.
# Copyright (C) 2015 Artyom V. Poptsov <poptsov.artyom@gmail.com>
#
# To the extent possible under law, the person who associated CC0 with
# this work has waived all copyright and related or neighboring rights
# to this work. See
# <https://creativecommons.org/publicdomain/zero/1.0/>
### Commentary:
# A simple Bash script that summarises up the data from a DSV file.
# The 1st column from the data file contains a name of a "kind" of
# object, and the 2nd column contains name of specific object of that
# kind. What we want is to get one "kind" name per a row in the 1st
# column, and the comma-separated list of objects.
### Code:
# Generate a temporary file name
TEST_DATA_FILE=$(mktemp)
# Prepare the data sample
echo "\
fruits banana
fruits orange
fruits apple
vegetables potato
vegetables carrot
vegetables broccoli
spices pepper
spices thyme \
" > $TEST_DATA_FILE
# Get the summary out of data.
awk '{print $1}' $TEST_DATA_FILE | awk '! seen[$0]++' | while read line; do
IFS=$'\n'
food=($(grep "$line" $TEST_DATA_FILE | awk '{print $2}'))
echo "$line" $(echo ${food[@]} | tr ' ' ',')
done
# Now the temporary file can be safely removed.
rm $TEST_DATA_FILE
### summary.sh ends here.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment