Search code examples
arraysawkkeyvaluepair

awk array that overtypes itself when printed


this is my first question so please let me know if I miss anything.

This is an awk script that uses arrays to make key-value pairs.

I have a file that has a header information separated by colons. The data is below it and separated by colons as well. My goal is to make key-value pairs that print out to a new file. I have everything set to be placed in arrays and it prints out almost perfectly.

Here is the input:

...:iscsi_name:iscsi_alias:panel_name:enclosure_id:canister_id:enclosure_serial_number
...:iqn.1111-00.com.abc:2222.blah01.blah01node00::11BLAH00:::

Here is the code:

#!/bin/awk -f
BEGIN {
    FS = ":"
}
{
    x = 1
    if (NR==1) {
        num_fields = NF ###This is done incase there are uneven head fields to data fields###
        while (x <= num_fields) {
            head[x] = $x
            x++
        }
    }
    y = 2
    while (y <= NR) {
        if (NR==y) {
            x = 1
            while (x <= num_fields) {
                data[x] = $x
                x++
            }
            x = 1
            while (x <= num_fields) {
                print head[x]"="data[x]
                x++
            }
        }
        y++
    }
}
END {
    print "This is the end of the arrays and the beginning of the test"
    print head[16]
    print "I am head[16]-"head[16]"- and now I'm going to overwrite everything"
    print "I am data[16]-"data[16]"- and I will not overwrite everything, also there isn't any data in data[16]"
}

Here is the output:

...
iscsi_name=iqn.1111-00.com.abc
iscsi_alias=2222.blah01.blah01node00
panel_name=
enclosure_id=11BLAH00
canister_id=
=nclosure_serial_number ### Here is my issue ###
This is the end of the arrays and the beginning of the test
enclosure_serial_number
- and now I'm going to overwrite everything
I am data[16]-- and I will not overwrite everything, also there isn't any data in data[16]

NOTE: data[16] is not at the end of a line, for some reason, there is an extra colon on the data lines, hence the num_fields note above

Why does head[16] overwrite itself? Is it that there is a newline (\n) at the end of the field? If so, how do I get rid of it? I have tried adding subtracting the last character, no luck. I have tried to limit the number of characters the array can take in on that field, no luck. I have tried many more ideas, no luck. Full Disclosure: I am relatively new to all of this, I might have messed up these previous fixes!

Does anyone have any ideas as to why this is happening? Thanks! -cheezter88


Solution

  • your script is unnecessarily complex. If you want to adjust the record size with the first row, do it so.

    (I replaced "..." prefix with "x")

    awk -F: 'NR==1 {n=split($0,h); next}   # populate header fields and record size
             NR==2 {for(i=1;i<=n;i++)      # do the assignment up to header size
                      print h[i]"="$i}' file
    
    x=x
    iscsi_name=iqn.1111-00.com.abc
    iscsi_alias=2222.blah01.blah01node00
    panel_name=
    enclosure_id=11BLAH00
    canister_id=
    enclosure_serial_number=
    

    if you want to do this for the rest of the records, remove the NR==2 condition,