Is there a possible limitation on lines? Trying on another config and getting blank lines:
,,,,
,,,,
,,,,
,,,,
,,,,
,,,,
,,,,
,,,,
So I did a
$ cat Configs/blahblah.Config | grep ^interface
It shows 159 lines since it also shows other interfaces besides just interface Vlan
example of results:
interface GigabitEthernet5/39
interface GigabitEthernet5/40
interface GigabitEthernet5/41
interface GigabitEthernet5/42
interface GigabitEthernet5/43
interface GigabitEthernet5/44
interface GigabitEthernet5/45
interface GigabitEthernet5/46
interface GigabitEthernet5/47
interface GigabitEthernet5/48
interface Vlan1
interface Vlan50
interface Vlan101
interface Vlan200
interface Vlan400
So to troubleshoot and trying to figure this out on my own in hopes to learn I started at print statements to validate the variables are being populated so I altered the code to look like this and the output is ugly but you can see there is stuff in there:
#! /usr/bin/awk -f
# awk -f findnet.awk Configs/.config > output.csv
BEGIN {
OFS=","
qq="\""
}
# FNR is the current record in the current file and increments each time a new record is read and starts at 0 each time a new input file is started
# edit: you probably need NR (instead of FNR) if you have multiple config files to parse
# this prints out the header line in the resulting csv
NR==1 {print "Interface", "IP address", "Description", "IP dhcp", "IP helper"}
# find any line that begins with interface and enter
# edit: 'interface' line is the indication of the start of a new block => set flag f to 1
# as the indication of a new block. Assign vlan a value from the second field
# skip the rest of the code and go to the 'next' record/line
/^interface/ { f=1; vlan=$2;next}
{ print vlan }
# edit: if we're in the block section (f is not 0) AND the line/record is not empty
# (NF!=0 or NF), enter the block lines prasing...
f && NF {
# If you find the string ip address then create variable ip and add
# edit: if 'ip address' is present on a current line... if 'ip' valiable is NOT empty, add to it
# the value of the last (NF) field prefixed with the OFS (,). If it's empty, prefix the
# last field with the duoble quote - beginning of the OFS-separated list
# Same logic for the other lines to be identified: description dhcp etc..
if (/ip address/) ip=(ip)?ip OFS $NF: qq $NF
print ip
if (/description/) desc=(desc)?desc OFS $0: qq $0
print desc
if (/dhcp relay/) dhcp=(dhcp)?dhcp OFS $NF: qq $NF
print dhcp
if (/helper-address/) help=(help)?help OFS $NF: qq $NF
print help
next
}
# got to this block if we're outside the 'block' - empty line (!NF) - see above
# print the accumulated variable from parsing the previous 'block - see above
# (ip)? ip qq:"" => means:if 'ip' is not empty, print 'ip' followed by a double quote
# (end of the OFS-separated list;
# if 'ip' is empty, output empty string ("")
# same for the other vars
{ print vlan, (ip)? ip qq:"", (desc)?desc qq:"", (dhcp)?dhcp qq:"", (help)?help qq:""
f=0; ip=desc=dhcp=help=""
}
Am i approaching this correctly?