From add006024594fdd479c5d9a177fc45db5abc3be6 Mon Sep 17 00:00:00 2001 From: Jephte CLAIN Date: Tue, 10 Jun 2014 11:32:34 +0400 Subject: [PATCH] bug dans sortcsv --- ulib/.ulib_version | 2 +- ulib/awk | 21 +++++++++++++++++---- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/ulib/.ulib_version b/ulib/.ulib_version index 425151f..87523dd 100644 --- a/ulib/.ulib_version +++ b/ulib/.ulib_version @@ -1 +1 @@ -40 +41 diff --git a/ulib/awk b/ulib/awk index b3ce51c..a40f9e4 100644 --- a/ulib/awk +++ b/ulib/awk @@ -1574,7 +1574,7 @@ function lsortcsv() { @ args -- "$@" && set -- "${args[@]}" || die "$args" local input="$1" - if [ -z "$input" ]; then + if [ -z "$input" -o "$input" == "-" ]; then input=/dev/stdin elif [ ! -f "$input" ]; then eerror "$input: fichier introuvable" @@ -1585,14 +1585,26 @@ function lsortcsv() { local padlen=${#padding} local headers + local -a tmpfiles + [ "$parse_headers" == "auto" ] && parse_headers=1 - if [ -n "$parse_headers" ]; then - [ -n "$headers" ] || headers="$(<"$input" awkrun skip:int="$skip" 'NR <= skip { next } { print; exit }')" + if [ -n "$parse_headers" -a -z "$headers" ]; then + if [ "$input" == /dev/stdin ]; then + # Si on lit depuis stdin, il faut faire une copie du flux dans un + # fichier temporaire pour calculer les en-tĂȘtes + local tmpinput + ac_set_tmpfile tmpinput "" __sortcsv_input0 "" __SORTCSV_DEBUG + array_add tmpfiles "$tmpinput" + cat >"$tmpinput" + input="$tmpinput" + fi + headers="$(<"$input" awkrun skip:int="$skip" 'NR <= skip { next } { print; exit }')" fi # faire le fichier de travail local tmpinput ac_set_tmpfile tmpinput "" __sortcsv_input "" __SORTCSV_DEBUG + array_add tmpfiles "$tmpinput" <"$input" >"$tmpinput" awkrun -f \ padding="$padding" padlen:int="$padlen" \ skip:int="$skip" parse_headers:int="$parse_headers" \ @@ -1616,6 +1628,7 @@ parse_headers && do_once("parse-headers") { # trier le fichier de travail local tmpsorted ac_set_tmpfile tmpsorted "" __sortcsv_sorted "" __SORTCSV_DEBUG + array_add tmpfiles "$tmpsorted" args=(# arguments de sort ${numeric_sort:+-n} ${ignore_case:+-f} ${reverse_sort:+-r} ${stable_sort:+-s} @@ -1633,7 +1646,7 @@ BEGIN { if (show_headers) print headerscsv } { print substr($0, padlen + 1) } ' - ac_clean "$tmpinput" "$tmpsorted" + ac_clean "${tmpfiles[@]}" return 0 }