Processing CSV with csvkit

опубликовано

Тэги: bash csv

$ cat data.csv 
name,amount
Linda,22
Joe,10
Jack,15
Joe,12
Linda,18
Jack,19

Get all Joe's records:

$ csvsql --query 'select * from data where name="Joe"' data.csv 
name,amount
Joe,10.0
Joe,12.0

Get amount of Joe's records:

$ csvsql --query 'select name, sum(amount) as total from data where name="Joe"' data.csv 
name,total
Joe,22.0

Save user records to separate files:

#!/bin/bash

RESULT=`csvsql --query "select distinct name from data" data.csv`

REALIFS=$IFS
IFS=$'\n'
USERS=($RESULT)
IFS=$REALIFS

for IDX in "${!USERS[@]}"
do
   :
   if [ $IDX -gt 0 ]; then
    CURRENT_USER=`echo "${USERS[$IDX]}" | tr -d '"'`
    if [ ${#CURRENT_USER} -gt 0 ]; then
      FILENAME="`echo "$CURRENT_USER" | sed 's/ /_/g; s/|/_/g; s/,/_/g'`.csv"
      echo "Saving user [$CURRENT_USER] data ..."
      csvsql --query "select * from data where name = '${CURRENT_USER}'" data.csv > $FILENAME
    fi
   fi
done

csvkit docs