possiblité de faire un mapping sur les attributs avec format/formatcsv

utiliser array_len() là ou c'est possible
This commit is contained in:
Jephté Clain 2014-10-02 14:53:05 +04:00
parent c4a3c4d6e2
commit 1a9b14a9dc
1 changed files with 40 additions and 35 deletions

View File

@ -539,18 +539,21 @@ function tl_format() {
fi fi
[ -n "$shell" ] && show_headers=1 [ -n "$shell" ] && show_headers=1
local match_attr="$(def_match_attr "$@")" local -a attrs mapattrs
local define_attrs= local attr mapattr
local i=1 attr
for attr in "$@"; do for attr in "$@"; do
define_attrs="$define_attrs attrs[$i] = \"$attr\"; " splitpair "$attr" mapattr attr
let i=$i+1 [ -n "$attr" ] || attr="$mapattr"
attr="${attr//:/}"; array_add attrs "$attr"
mapattr="${mapattr//:/}"; array_add mapattrs "$mapattr"
done done
local match_attr="$(def_match_attr "${attrs[@]}")"
awkrun -f show_headers="$show_headers" asep="$asep" vsep="$vsep" qsep="$qsep" escape="$escape" \ awkrun -f show_headers:int="$show_headers" asep="$asep" vsep="$vsep" qsep="$qsep" escape="$escape" \
shell="$shell" cmd="$cmd" begincmd="$begincmd" endcmd="$endcmd" localvars="$localvars" ' shell:int="$shell" cmd="$cmd" begincmd="$begincmd" endcmd="$endcmd" localvars:int="$localvars" \
attrs[@] mapattrs[@] '
function reset_values() { function reset_values() {
for (i = 1; i <= length(attrs); i++) { for (i = 1; i <= attrs_count; i++) {
attr = norm_attr(attrs[i]) attr = norm_attr(attrs[i])
values[attr] = "" values[attr] = ""
} }
@ -565,21 +568,21 @@ function dump_headers() {
if (shell) { if (shell) {
if (localvars) { if (localvars) {
print "local attributes index" print "local attributes index"
for (i = 1; i <= length(attrs); i++) { for (i = 1; i <= attrs_count; i++) {
attr = norm_attr(attrs[i]) attr = norm_attr(mapattrs[i])
print "local " attr print "local " attr
} }
} }
line = line "attributes=(" line = line "attributes=("
for (i = 1; i <= length(attrs); i++) { for (i = 1; i <= attrs_count; i++) {
attr = norm_attr(attrs[i]) attr = norm_attr(mapattrs[i])
if (i > 1) line = line " " if (i > 1) line = line " "
line = line quote_shell(attr) line = line quote_shell(attr)
} }
line = line ")" line = line ")"
} else { } else {
for (i = 1; i <= length(attrs); i++) { for (i = 1; i <= attrs_count; i++) {
attr = attrs[i] attr = mapattrs[i]
if (i > 1) line = line asep if (i > 1) line = line asep
line = line attr line = line attr
} }
@ -598,14 +601,14 @@ function __should_quote(s) {
function dump_values(atbegin, value) { function dump_values(atbegin, value) {
if (shell) { if (shell) {
print "index=" nr print "index=" nr
for (i = 1; i <= length(attrs); i++) { for (i = 1; i <= attrs_count; i++) {
attr = norm_attr(attrs[i]) attr = norm_attr(attrs[i])
print attr "=" values[attr] print attr "=" values[attr]
} }
if (!atbegin && cmd != "") print cmd if (!atbegin && cmd != "") print cmd
} else { } else {
line = "" line = ""
for (i = 1; i <= length(attrs); i++) { for (i = 1; i <= attrs_count; i++) {
attr = norm_attr(attrs[i]) attr = norm_attr(attrs[i])
if (i > 1) line = line asep if (i > 1) line = line asep
value = values[attr] value = values[attr]
@ -635,7 +638,6 @@ function parse_value(line) {
'"$match_attr"' '"$match_attr"'
BEGIN { BEGIN {
'"$define_attrs"'
if (show_headers) dump_headers() if (show_headers) dump_headers()
reset_values() reset_values()
nr = -1 nr = -1
@ -721,33 +723,33 @@ function tl_parsecsv() {
awkrun -f \ awkrun -f \
basedn="$basedn" rdnattr="$rdnattr" \ basedn="$basedn" rdnattr="$rdnattr" \
skip_lines="$skip_lines" parse_headers="$parse_headers" "headers[@]=headers" \ skip_lines:int="$skip_lines" parse_headers:int="$parse_headers" headers[@] \
vsep="$vsep" ' vsep="$vsep" '
BEGIN { skip_lines = skip_lines + 0; parse_headers = parse_headers + 0 }
NR <= skip_lines { next } NR <= skip_lines { next }
parse_headers { parse_headers {
array_parsecsv(headers, $0) array_parsecsv(headers, $0)
headers_count = array_len(headers)
parse_headers = 0 parse_headers = 0
next next
} }
{ {
array_parsecsv(attrs, $0) array_parsecsv(attrvalues, $0)
rdnindex = key_index(rdnattr, headers) rdnindex = key_index(rdnattr, headers)
rdnvalue = attrs[rdnindex] rdnvalue = attrvalues[rdnindex]
if (basedn != "") { if (basedn != "") {
print "dn: " rdnattr "=" rdnvalue "," basedn print "dn: " rdnattr "=" rdnvalue "," basedn
} else { } else {
print "dn: " rdnvalue print "dn: " rdnvalue
} }
nbheaders = length(headers) for (i = 1; i <= headers_count; i++) {
for (i = 1; i <= nbheaders; i++) {
attr = attrs[i]
split(attr, values, vsep)
attr = headers[i] attr = headers[i]
if (basedn == "" && attr == rdnattr) { if (basedn == "" && attr == rdnattr) {
continue # ne pas écrire deux fois le même attribut continue # ne pas écrire deux fois le même attribut
} }
for (j = 1; j <= length(values); j++) { attrvalue = attrvalues[i]
split(attrvalue, values, vsep)
values_count = array_len(values)
for (j = 1; j <= values_count; j++) {
print attr ": " values[j] print attr ": " values[j]
} }
} }
@ -781,12 +783,12 @@ function tl_parsecsvmod() {
awkrun -f \ awkrun -f \
basedn="$basedn" rdnattr="$rdnattr" \ basedn="$basedn" rdnattr="$rdnattr" \
skip_lines="$skip_lines" parse_headers="$parse_headers" "headers[@]=headers" \ skip_lines:int="$skip_lines" parse_headers:int="$parse_headers" headers[@] \
vsep="$vsep" ' vsep="$vsep" '
BEGIN { skip_lines = skip_lines + 0; parse_headers = parse_headers + 0 }
NR <= skip_lines { next } NR <= skip_lines { next }
parse_headers { parse_headers {
array_parsecsv(headers, $0) array_parsecsv(headers, $0)
headers_count = array_len(headers)
parse_headers = 0 parse_headers = 0
next next
} }
@ -794,10 +796,9 @@ parse_headers {
array_parsecsv(attrvalues, $0) array_parsecsv(attrvalues, $0)
rdnindex = key_index(rdnattr, headers) rdnindex = key_index(rdnattr, headers)
rdnvalue = attrvalues[rdnindex] rdnvalue = attrvalues[rdnindex]
nbheaders = length(headers)
# déterminer d"abord s"il faut écrire l"objet # déterminer d"abord s"il faut écrire l"objet
printobj = 0 printobj = 0
for (i = 1; i <= nbheaders; i++) { for (i = 1; i <= headers_count; i++) {
attr = headers[i] attr = headers[i]
if (attr == rdnattr) continue if (attr == rdnattr) continue
attrvalue = attrvalues[i] attrvalue = attrvalues[i]
@ -814,7 +815,7 @@ parse_headers {
} }
# maintenant, écrire l"objet le cas échéant # maintenant, écrire l"objet le cas échéant
if (printobj) { if (printobj) {
for (i = 1; i <= nbheaders; i++) { for (i = 1; i <= headers_count; i++) {
attr = headers[i] attr = headers[i]
if (attr == rdnattr) continue if (attr == rdnattr) continue
attrvalue = attrvalues[i] attrvalue = attrvalues[i]
@ -829,7 +830,8 @@ parse_headers {
} }
print changetype ": " attr print changetype ": " attr
split(attrvalue, values, vsep) split(attrvalue, values, vsep)
for (j = 1; j <= length(values); j++) { values_count = array_len(values)
for (j = 1; j <= values_count; j++) {
print attr ": " values[j] print attr ": " values[j]
} }
print "-" print "-"
@ -898,6 +900,9 @@ awkrun [args]
exemple l'option -i de sed exemple l'option -i de sed
format [options] attrs... format [options] attrs...
Formater le flux au format ldif en données tabulaires. Formater le flux au format ldif en données tabulaires.
Chaque attribut peut être de la forme newname:oldname pour avoir des
en-têtes avec un nom différent des noms des attributs. L'effet est le même
qu'avec awkcsv -m
-h, --show-headers -h, --show-headers
Afficher les en-têtes. Par défaut, les en-têtes ne sont pas affichés. Afficher les en-têtes. Par défaut, les en-têtes ne sont pas affichés.
-F ASEP -F ASEP