Des scripts pour imprimer les données dans TSV sous la forme d'un layout prédéfini - retour accueil
git clone git://bebou.netlib.re/tsv2layout
Log | Files | Refs | README |
commit eb7123bbc06f2ef19710f352c52a08ddfc7dc41d parent a7c6496e779f5441b9e2f5147ac28de4e01c7ad1 Auterice: Arthur Pons <arthur.pons@unistra.fr> Date: Thu, 19 Sep 2024 16:59:05 +0200 Renommage des fichiers et factorisation des tests Diffstat:
A | tests/layout.printf | | | 4 | ++++ |
A | tests/reference.printf | | | 40 | ++++++++++++++++++++++++++++++++++++++++ |
M | tests/test.sh | | | 17 | ++++++++++------- |
R | tsv2layout -> tsv2layout.heredoc | | | 0 | |
A | tsv2layout.printf | | | 22 | ++++++++++++++++++++++ |
D | tsv2layout.printf2 | | | 23 | ----------------------- |
6 files changed, 76 insertions(+), 30 deletions(-)
diff --git a/tests/layout.printf b/tests/layout.printf @@ -0,0 +1,4 @@ +[$index] +$firstname $lastname +mailto:$email +Born on $dateofbirth, is a $jobtitle diff --git a/tests/reference.printf b/tests/reference.printf @@ -0,0 +1,40 @@ +[1] +Shelby Terrell +mailto:elijah57@example.net +Born on 1945-10-26, is a Games developer +[] + Summers +mailto:bethany14@example.com +Born on 1910-03-24, is a +[3] +Kristine Travis +mailto:bthompson@example.com +Born on 1992-07-02, is a Homeopath +[4] +Yesenia +mailto:kaitlinkaiser@example.com +Born on 2017-08-03, is a Market researcher +[5] +Lori Todd +mailto:buchananmanuel@example.net +Born on , is a Veterinary surgeon +[6] +Erin Day +mailto:tconner@example.org +Born on 2015-10-28, is a Waste management officer +[7] +Katherine Buck +mailto:conniecowan@example.com +Born on 1989-01-22, is a Intelligence analyst +[8] +Ricardo Hinton +mailto:wyattbishop@example.com +Born on 1924-03-26, is a Hydrogeologist +[9] +Dave Farrell +mailto: +Born on 2018-10-06, is a Lawyer +[10] +Isaiah Downs +mailto:virginiaterrell@example.org +Born on 1964-09-20, is a Engineer, site diff --git a/tests/test.sh b/tests/test.sh @@ -12,13 +12,16 @@ issame() { diff -q "$1" "$2" } -tmpd=$(mktemp -d) -trap "rm -rf $tmpd" EXIT +test() { + f=$(mktemp) + < tests/data ./tsv2layout.$1 tests/layout.$1 > $f + issame tests/reference.$1 $f \ + && _success "tsv2layout.$1 test passed" \ + || _die "tsv2layout.$1 test failed" +} -< tests/data ./tsv2layout tests/layout.heredoc > $tmpd/1 -#< tests/data ./tsv2layout.printf2 tests/layout.printf > $tmpd/2 +trap "rm -f $f" EXIT -issame tests/reference.heredoc $tmpd/1 \ - && _success "printlayout test passed" \ - || _die "printlayout test failed" +test heredoc +test printf diff --git a/tsv2layout b/tsv2layout.heredoc diff --git a/tsv2layout.printf b/tsv2layout.printf @@ -0,0 +1,22 @@ +#! /bin/sh + +tmpd=$(mktemp -d) +trap "rm -rf $tmpd" EXIT + +layout=$(< $1 sed -E 's/\$[a-zA-Z]+/%s/g') +nbargs=$(echo "$layout" | grep -o '%s' | wc -l) +maxargs=$(echo "4096/$nbargs*$nbargs" | bc) + +cat > $tmpd/in + +< $tmpd/in head -n1 | tr ' ' '\n' | nl -n ln -w1 > $tmpd/vars + +for var in $(< $1 grep -wEo '\$[a-zA-Z]+' | tr -d '$');do + grep "$var" $tmpd/vars +done | + cut -f1 | paste -s -d',' | + xargs -I{} awk -F'\t' -v col={} ' + BEGIN{getline;n=split(col,cols,",")} + { for (i=1;i<n+1;i++) { print $cols[i] } }' $tmpd/in | + xargs -d'\n' -n$maxargs printf "$layout\n" + diff --git a/tsv2layout.printf2 b/tsv2layout.printf2 @@ -1,23 +0,0 @@ -#! /bin/sh - -tmpd=$(mktemp -d) - -layout=$(< $1 sed -E 's/\$[a-zA-Z]+/%s/g') -nbargs=$(echo "$layout" | grep -o '%s' | wc -l) -maxargs=$(echo "4096/$nbargs*$nbargs" | bc) - -cat > $tmpd/in - -< $tmpd/in head -n1 | tr ' ' '\n' | nl -n ln -w1 > $tmpd/vars - -for var in $(< $1 grep -wEo '\$[a-zA-Z]+' | tr -d '$');do - grep "$var" $tmpd/vars -done | - cut -f1 | paste -s -d',' | - xargs -I{} awk -F'\t' -v col={} ' - BEGIN{getline;split(col,cols,",")} - { for (i=1;i<5;i++) { print $cols[i] } }' $tmpd/in | - xargs -d'\n' -n$maxargs printf "$layout\n" - -rm -rf $tmpd -