Add import from csv function - dossier - console collection manager DIR Log DIR Files DIR Refs DIR Tags DIR README DIR LICENSE --- DIR commit c40faa46c5be77e015efa65ccc8d1a08995b6e33 DIR parent 5b178df77317dffde3e401fce4965076b96d6553 HTML Author: Solene Rapenne <solene@perso.pw> Date: Thu, 2 Aug 2018 16:22:00 +0200 Add import from csv function Diffstat: M dossier | 30 ++++++++++++++++++++++++++++++ M dossier.1 | 9 ++++++++- 2 files changed, 38 insertions(+), 1 deletion(-) --- DIR diff --git a/dossier b/dossier @@ -58,6 +58,32 @@ show_attributes_values() { exit 0 } +# import data from a csv +# $1 is the file to import +import_csv() { + awk -F "," '{ + if(NR==1) { + for(i=1;i<=NF;i=i+1) { + headers[i] = $i + } + } else { + out = $1; + for(i=2;i<=NF;i=i+1) { + if(! match($i,/^""$/) && $i !="") { + out = out " "headers[i]" "$i; + } + } + if(out==$1) { + print "empty" + } else { + out=out "\n" + print out + system("dossier "out) + } + } + }' "$1" +} + # export the data in csv format "data","data","data" # we assume it'll works with the dataset export_csv() { @@ -218,6 +244,9 @@ usage() { "Export data as CSV" \ ": dossier export" \ "" \ + "Import data from CSV" \ + ": dossier import file.csv" \ + "" \ "Show collections, register collections, switch current collection" \ ": dossier collections [register path name] [name]" \ "" \ @@ -268,6 +297,7 @@ register() { fi } +if [ "$1" = "import" ] && [ -f "${OLDPWD}/${2}" ]; then import_csv "${OLDPWD}/${2}" ; fi if [ "$1" = "export" ] ; then export_csv ; fi if [ "$1" = "rm" ] && [ "$#" -eq 2 ] ; then delete "$2" ; fi if [ "$1" = "help" ] ; then usage ; fi DIR diff --git a/dossier.1 b/dossier.1 @@ -6,7 +6,7 @@ .Nd a console collection manager .Sh SYNOPSIS .Nm -.Op Cm show | Cm help | Cm export | Cm collections | Cm rm | Cm search | Ar item Op attribute value +.Op Cm show | Cm import Ar file | Cm help | Cm export | Cm collections | Cm rm | Cm search | Ar item Op attribute value .Sh DESCRIPTION .Pp .Nm @@ -37,6 +37,13 @@ Remove item from the collection. .It Nm Cm export Export the data of the current collection as CSV (Comma Separated Values). +.It Nm Cm import Ar file +Import the data from +.Ar file +using the exact same format as the export (no newline in a field, fields name in +the first line, values with spaces should be quoted). This is very useful for a +batch import or if you want to export the data to a spreadsheet tool to +manipulate the values and then importing back. .It Nm Cm collections Oo collection Oc | Oo register Pa full-path Ar collection-name Oc the command .Cm collections