1

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

  • 1
    tl;dr but the title but windows type line endings may be the cause (`\r\n` vs. just `\n`, use `dos2unix` or `sub(/\r/,"",$NF)`in awk). – James Brown Nov 20 '17 at 18:11
  • BTW, your script has single quotes in last two print calls. – thanasisp Nov 20 '17 at 19:24
  • Just saw this after I posted the comment below, thank you! Transferring everything over to Linux. I was literally was banging my head against the desk and it's just silly Windows. – James Crafton Nov 20 '17 at 21:44

1 Answers1

0

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,

karakfa
  • 66,216
  • 7
  • 41
  • 56
  • Thank you for the quick reply! Also, thank you for showing a much more concise version of this code, this is much less complicated and has a lot fewer moving parts. I am still having the issue with the last line where everything after the array is copied over. I am beginning to think that it is something to do with my system. I am on Windows and running Cygwin64 Terminal, could this cause the issues I am speaking of? – James Crafton Nov 20 '17 at 21:41
  • Nevermind, James Brown answered the question, thank you all for your help and quick responses! – James Crafton Nov 20 '17 at 21:45