[lug] awk question
John E. Koontz
koontz at boulder.nist.gov
Tue Jan 28 12:12:54 MST 2003
At 08:55 AM 1/28/2003 -0700, you wrote:
>Thanks! I think I'll still try the exercise in pain that John Koontz
>suggested since I'd ultimately like to automate this... But your
>recommendation will help me get the initial meat-n-potatoes out today.
A little more painful than I recalled, the main pains being:
1) Can't regroup in field variables, because fields are reparsed (actually
a useful thing ...).
So, you put the fixed up fields in an array.
2) But, once you put it in an array, the lack of a looping structure for
use in prints requires
a loop to reassemble things.
3) I never have been able to grasp the workings of OFS, the output field
separator.
BEGIN { FS = ","
myfs = "|"
}
{ print "On input " NF " fields: " $0 }
{ j = 1
f[j] = $1
for (i = 2; i <= NF; i++) {
if (f[j] ~ /^".*[^"]$/) {
f[j] = f[j] FS $i
} else {
j++
f[j] = $i
}
# print "j = " j " i = " i " f[j] = " f[j]
}
}
{ record = f[1]
for (i = 2; i <= j; i++) {
record = record myfs f[i]
}
}
{ print "On output " j " fields: " record }
John E. Koontz
NIST 896.04 PCSG
303-497-5180
N39° 59' 42.1" W 105° 15' 49.7"
More information about the LUG
mailing list