Sponsored Content
Top Forums Shell Programming and Scripting Parsing cisco cfg to export as csv Post 302953107 by djzah on Tuesday 25th of August 2015 11:21:10 AM
Old 08-25-2015
Is there a possible limitation on lines? Trying on another config and getting blank lines:
Code:
,,,,
,,,,
,,,,
,,,,
,,,,
,,,,
,,,,
,,,,

So I did a
Code:
$ cat Configs/blahblah.Config | grep ^interface

It shows 159 lines since it also shows other interfaces besides just interface Vlan
example of results:
Code:
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:
Code:
#! /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?
 

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

unix script to export data from csv file to oracle database

Hello people, Need favour. The problem I have is that, I need to develop a unix shell script that performs recurring exports of data from a csv file to an oracle database. Basically, the csv file contains just the first name and last name will be dumped to an Unix server. The data from these... (3 Replies)
Discussion started by: vinayagan
3 Replies

2. Shell Programming and Scripting

Parsing a csv file

I am trying to parse a csv file in the below 'name-value pair' format and then use the values corresponding to the name. Type:G,Instance:instance1,FunctionalID:funcid,Env:dev,AppName:appname... (6 Replies)
Discussion started by: chiru_h
6 Replies

3. Shell Programming and Scripting

2 problems: Mailing CSV file / parsing CSV for display

I have been trying to find a good solution for this seemingly simple task for 2 days, and I'm giving up and posting a thread. I hope someone can help me out! I'm on HPUX, using sqlplus, mailx, awk, have some other tools available, but can't install stuff that isn't already in place (without a... (6 Replies)
Discussion started by: soldstatic
6 Replies

4. Shell Programming and Scripting

Parsing a CSV File

Hey guys, I'm in the process of learning PHP and BASH scripting. I'm getting there, slowly ;) I would like some help with parsing a CSV file. This file contains a list of hostnames, dates, and either Valid, Expired, or Expired Soon in the last column. Basically, I want to parse the file,... (12 Replies)
Discussion started by: dzl
12 Replies

5. Shell Programming and Scripting

How to export named to CSV?

I am really shooting for the moon here but I really want to be able to break down our named.conf file into a CSV file so I can easily see what options are for each zone. Here are two basic entries as examples: zone "mywiseguys.com" { type slave; file... (9 Replies)
Discussion started by: djzah
9 Replies

6. Shell Programming and Scripting

Help with Parsing a CSV File

Hello All, I have an input CSV file like below, where first row data can be in different position after every run of the tool, i.e. pzTest in below example is in column 1, but it can be also in 3 column and same for all the headers in the first row. pzTest, pzExtract, pxUpdate, pzInfo... (1 Reply)
Discussion started by: asirohi
1 Replies

7. Linux

Parsing - export html table data as .csv file?

Hi all, Is there any out there have a brilliant idea on how to export html table data as .csv or write to txt file with separated comma and also get the filename of link from every table and put one line per rows each table. Please see the attached html and PNG of what it looks like. ... (7 Replies)
Discussion started by: lxdorney
7 Replies

8. Shell Programming and Scripting

Script to Gather data from logs and export to a CSV file

Greetings, After a few hours of trial and error, I decide to ask for some help. I am new to AWK and shell script, so please don't laugh :p I made the below script, to gather data from some logs and have the output into a CSV file : #!/bin/sh #Script to collect Errors ... (9 Replies)
Discussion started by: Yagami_Sama
9 Replies
All times are GMT -4. The time now is 06:45 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy