From 377ca5e82f1598cfa8d9ef55f4571847f6b9c6a4 Mon Sep 17 00:00:00 2001 From: Jephte Clain Date: Thu, 24 Aug 2017 12:15:16 +0400 Subject: [PATCH] =?UTF-8?q?impl=C3=A9menter=20awkcsv2fsv.=20corriger=20un?= =?UTF-8?q?=20bug=20avec=20les=20outils=20qui=20utilisaient=20cawkrun=20?= =?UTF-8?q?=C3=A0=20l'int=C3=A9rieur=20d'une=20fonction=20l*?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/ulib/awk | 75 +++++++++++++++++++++++++++++++++++++++++++--------- uawk | 16 +++++++++-- 2 files changed, 77 insertions(+), 14 deletions(-) diff --git a/lib/ulib/awk b/lib/ulib/awk index c242e6b..2a289b0 100644 --- a/lib/ulib/awk +++ b/lib/ulib/awk @@ -841,7 +841,7 @@ function lawkcsv() { reset_fields= fi - awkrun -f \ + lawkrun -f \ skip_lines:int="$skip" parse_headers:int="$parse_headers" \ autosep:int="$autosep" colsep="$colsep" qchar="$qchar" echar="$echar" \ show_headers:int="$show_headers" reset_fields:int="$reset_fields" \ @@ -978,7 +978,7 @@ function lgrepcsv() { headers=("$@") if [ -z "${headers[*]}" ]; then parse_headers=1 - array_from_lines headers "$(awkrun -f lskip:int="$skip" 'NR <= lskip { next } + array_from_lines headers "$(lawkrun -f lskip:int="$skip" 'NR <= lskip { next } { count = array_parsecsv(__fields, $0) for (i = 1; i <= count; i++) { @@ -1077,7 +1077,7 @@ function lawkfsv2csv() { done shift - awkrun -f \ + lawkrun -f \ skip_lines:int="$skip" trim_values:int="$rtrim" show_headers:int="$show_headers" \ headers[@] starts[@] sizes[@] \ "$__AWKCSV_FUNCTIONS"' @@ -1109,6 +1109,57 @@ function awkfsv2csv() { LANG=C lawkfsv2csv "$@"; } ################################################################################ +__AWKCSV2FSV_HELP="\ +Transformer un flux csv en fsv (colonnes à largeurs fixes) + +Chaque argument doit être de la forme field:size" + +function lawkcsv2fsv() { + local -a args fieldsizes + if parse_opts @ args -- "$@"; then + set -- "${args[@]}" + else + eerror "$args" + return 1 + fi + + local -a fields sizes + local fieldsize field size + while [ $# -gt 0 -a "$1" != "--" ]; do + fieldsize="$1" + shift + + splitpair "$fieldsize" field size + [ -n "$field" ] || { + eerror "field est requis" + return 1 + } + [ -n "$size" ] || size=1 + array_add fields "$field" + array_add sizes "$size" + done + shift + + lawkcsv -v fields[@] -v sizes[@] -a ' +{ + line = "" + for (i = 1; i <= fields_count; i++) { + size = sizes[i] + 0 + value = get(fields[i]) + while (length(value) < size) value = value " " + if (length(value) > size) value = substr(value, 1, size) + line = line value + } + print line +} +' -- "$@" +} + +function cawkcsv2fsv() { LANG=C lawkcsv2fsv "$@"; } +function awkcsv2fsv() { LANG=C lawkcsv2fsv "$@"; } + +################################################################################ + __MERGECSV_HELP="\ Fusionner deux fichiers csv en faisant la correspondance sur la valeur d'un champ, qui est la clé @@ -1264,14 +1315,14 @@ function lmergecsv() { [ "$parse_headers" == "auto" ] && parse_headers=1 if [ -n "$parse_headers" ]; then - [ -n "$lheaders" ] || lheaders="$(<"$lfile" awkrun lskip:int="$lskip" 'NR <= lskip { next } { print; exit }')" - [ -n "$rheaders" ] || rheaders="$(<"$rfile" awkrun lskip:int="$lskip" 'NR <= lskip { next } { print; exit }')" + [ -n "$lheaders" ] || lheaders="$(<"$lfile" lawkrun lskip:int="$lskip" 'NR <= lskip { next } { print; exit }')" + [ -n "$rheaders" ] || rheaders="$(<"$rfile" lawkrun lskip:int="$lskip" 'NR <= lskip { next } { print; exit }')" fi # faire le fichier de travail pour lfile local tmpleft ac_set_tmpfile tmpleft "" __mergecsv_left "" __MERGECSV_DEBUG - <"$lfile" awkrun -f \ + <"$lfile" lawkrun -f \ padding="$padding" padlen:int="$padlen" \ parse_headers:int="$parse_headers" ignore_case:int="$ignore_case" \ lskip:int="$lskip" lkey="$lkey" \ @@ -1295,7 +1346,7 @@ parse_headers && do_once("parse-headers") { # faire le fichier de travail pour rfile local tmpright ac_set_tmpfile tmpright "" __mergecsv_right "" __MERGECSV_DEBUG - <"$rfile" awkrun -f \ + <"$rfile" lawkrun -f \ padding="$padding" padlen:int="$padlen" \ parse_headers:int="$parse_headers" ignore_case:int="$ignore_case" \ rskip:int="$rskip" rkey="$rkey" \ @@ -1409,7 +1460,7 @@ parse_headers && do_once("parse-headers") { ac_set_tmpfile tmpmerged "" __mergecsv_merged "" __MERGECSV_DEBUG csort -s -k 1,$(($padlen + 1)) "$tmpleft" "$tmpright" >"$tmpmerged" - <"$tmpmerged" awkrun -f \ + <"$tmpmerged" lawkrun -f \ padlen:int="$padlen" \ parse_headers:int="$parse_headers" ignore_case:int="$ignore_case" \ lheaderscsv="$lheaders" lkey="$lkey" lprefix="$lprefix" \ @@ -1700,14 +1751,14 @@ function lsortcsv() { cat >"$tmpinput" input="$tmpinput" fi - headers="$(<"$input" awkrun skip:int="$skip" 'NR <= skip { next } { print; exit }')" + headers="$(<"$input" lawkrun 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 \ + <"$input" >"$tmpinput" lawkrun -f \ padding="$padding" padlen:int="$padlen" \ skip:int="$skip" parse_headers:int="$parse_headers" \ key="$key" \ @@ -1740,7 +1791,7 @@ parse_headers && do_once("parse-headers") { # résultat [ -n "$output" ] || output=/dev/stdout - stdredir "$tmpsorted" "$output" "" awkrun -f \ + stdredir "$tmpsorted" "$output" "" lawkrun -f \ padlen:int="$padlen" \ headerscsv="$headers" show_headers:int="$show_headers" \ ' @@ -2060,7 +2111,7 @@ function lprintcsv() { [ -n "$output" ] || output=/dev/stdout values=("$@") - stdredir "" ">>$output" "" awkrun -f fields[@] show_headers:int="$show_headers" values[@] ' + stdredir "" ">>$output" "" lawkrun -f fields[@] show_headers:int="$show_headers" values[@] ' BEGIN { if (show_headers) array_printcsv(fields) if (fields_count > 0) count = fields_count diff --git a/uawk b/uawk index bd9716a..c59351a 100755 --- a/uawk +++ b/uawk @@ -1,7 +1,7 @@ #!/bin/bash # -*- coding: utf-8 mode: sh -*- vim:sw=4:sts=4:et:ai:si:sta:fenc=utf-8 -TOOLS=(awkrun awkcsv grepcsv awkfsv2csv mergecsv sortcsv dumpcsv printcsv) +TOOLS=(awkrun awkcsv grepcsv awkfsv2csv awkcsv2fsv mergecsv sortcsv dumpcsv printcsv) if [ "$#" -eq 1 -a "$1" == --nutools-makelinks ]; then # créer les liens scriptname="$(basename "$0")" @@ -49,6 +49,7 @@ run) tool=awkrun;; csv) tool=awkcsv;; grep) tool=grepcsv;; fsv2csv|fsv) tool=awkfsv2csv;; +csv2fsv) tool=awkcsv2fsv;; merge) tool=mergecsv;; sort) tool=sortcsv;; dump) tool=dumpcsv;; @@ -95,12 +96,23 @@ elif [ "$tool" == "awkfsv2csv" ]; then ${Clang:+note: ce script ne tient pas compte de la locale: il travaille avec LANG=C } USAGE - $scriptname [options] -- [header:sizes...] [-- inputfiles...] + $scriptname [options] -- [header:size,...] [-- inputfiles...] $__AWKFSV2CSV_HELP" "${prefix}awkfsv2csv" "$@" +elif [ "$tool" == "awkcsv2fsv" ]; then + [ $# -eq 1 -a "$1" == --help ] && exit_with uecho "$scriptname: wrapper autour de awk pour transformer un fichier à colonnes fixes en fichier csv +${Clang:+note: ce script ne tient pas compte de la locale: il travaille avec LANG=C +} +USAGE + $scriptname [options] -- [field:size,...] [-- inputfiles...] + +$__AWKCSV2FSV_HELP" + + "${prefix}awkcsv2fsv" "$@" + elif [ "$tool" == "mergecsv" ]; then [ $# -eq 1 -a "$1" == --help ] && exit_with uecho "$scriptname: fusionner deux fichiers csv sur un champ commun ${Clang:+note: ce script ne tient pas compte de la locale: il travaille avec LANG=C