Change (length($i) > map[$i])
to (length($i) > map[i])
and substr($i, 1, map[$i])
to substr($i, 1, map[i])
.
Like this:
gawk -F"|" -- '
BEGIN {
map[1]=10
map[2]=20
map[3]=60
map[4]=60
map[5]=3
map[6]=60
map[7]=3
OFS="|"
}
{
for(i = 1; i <= NF; i++) {
if (length($i) > map[i]) {
$i = substr($i, 1, map[i])
}
}
print;
}
'
$i
refers to the contents of field number i
, but the contents of that field is not the index of map
.