I am using awk to read lines from a CSV file then put data into other files. These other files are named using the value of a certain column. Column 7 is a name such as "att" or "charter" . I want to end up with file names with the value of column 7 appended to them, like this:
stockton-1-migrate-att.cfg
stockton-2-migrate-att.cfg
stockton-1-migrate-charter.cfg
stockton-2-migrate-charter.cfg
I am getting these files alright, but I am ending up with a couple extra files as if column 7 was not populated.
stockton-migrate-1-.cfg
stockton-migrate-2-.cfg
The last part of the file name after the 3rd "-" comes from the $7 variable which I have as part of the output file name. I don't understand why my script is creating these files that end in "-.cfg" since $7 is always populated with data. Also, I instruct the script not to run through the while loop when $4 equals "d" and $7 equals "none".
Here is a snippet of my CSV file, notice that line 17 has "d" in $4 and "none" in $7.
1,k,,,Keystone, RENO DOWNTOWN,charter1,,
2,k,,,Keystone, SPARKS NORTH,charter1,,
7,k,,,Keystone, WASHOE MED CENTER,att1,,
17,k,,d,Keystone, RENO BRISAS,none,,
Here is my code. Is there something I am doing wrong? I am getting the data I need but I am ending up with extra data that I don't want which makes me think there is a better way of doing this.
BEGIN {FS = ",";read_header=0}
# Read the header row that has the field keys
{if(read_header==1) {
#read values of variables to replace
for (i=1; i<=num_fields; i=i+1) value=$i;
{if($4 == "d") {
output_file = "no routes.txt";
while (getline < "nodata.cfg" > 0) {
for (i=1; i<=num_fields; i=i+1) gsub(header,value);
print $0 >> output_file;
}
close("nodata.cfg")
}
}
{if($4 != "d" && $7 != "none") {
output_file = "stockton-1-migrate-"$7".cfg";
output_file2 = "stockton-2-migrate-"$7".cfg";
while (getline < "mls-migration-template-s1.cfg" > 0) {
for (i=1; i<=num_fields; i=i+1) gsub(header,value);
print $0 >> output_file;
}
close("mls-migration-template-s1.cfg")
while (getline < "mls-migration-template-s2.cfg" > 0) {
for (i=1; i<=num_fields; i=i+1) gsub(header,value);
print $0 >> output_file2;
}
close("mls-migration-template-s2.cfg")
}
}
}
}
{if(read_header==0) {
#read the names of variables to replace
num_fields=NF;
for (i=1; i<=NF; i=i+1) {header=$i}
read_header=1}}