Hi,
Despite reading the Conditional Statements chapter in the O'Reilly Sed & Awk book several times and looking at numerous examples, I cannot for the life of me get any kind of if ... else statement to work in my awk scripts! My scripts work perfectly (as they are written at least) and do what they should until I add any if statement(s), at which point I just get syntax errors at every 'if' and every 'else'.
Can anyone shed some light on this please? I am using gawk in DOS under Windows XP Pro SP3 (not my choice of OS, but it pays the bills).
BEGIN {FS="[ \t]*,[ \t]*";
OFS=",";
print "Finding good records..."
}
NR == 1 { new_fname = gensub(/\.csv$/, "", 1, FILENAME) "_good.csv"; }
$36 ~ /N/ { for (i = 1; i<= NF; i++) {
gsub(/^ /, "", $i);
gsub(/ $/, "", $i);
gsub(/"/, "", $i);
}
}
output = ""
output = sprintf ("%s,%s,%s", $1, $38, $39);
if ( $40 == /^ / && $41 == /^ / ) {
output = sprintf("%s,%s,%s,%s,", output, $42, $43, $44);
}
else {
output = sprintf("%s,%s,%s,%s,%s,%s,", output, $40, $41, $42, $43, $44);
}
if ( $55 ~ /MR|MISS|MS|MRS/ ) {
output = sprintf("%s, %s %s %s %s", output, $59, $55, $56, $57, $58);
}
else if ( $55 ~ /Applicant/ && !$58 ) {
output = sprintf("%s, %s %s %s %s", output, $60, $56, $57, $58, $59);
}
END {
printf("%s\n",output);
print "Finished! Output sent to '"new_fname"'"
}
Basically I am parsing a csv file which has a few empty fields (they are always in the same place, but are not on every line) that I wish to capture and remove from the output, this is what the if ... else statements do. I have tried other methods but this seems like the best (or only) way to do it, combined with sprintf.