Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Very New to Python
#1
Greetings all,
I am very new to python. I have a project I am working on and figured Python might be a good tool. I have a directory with a large number of configuration files in them and I need to get the files turned into a format that is easily searchable.

in every file there are many lines but for what I am looking to do, each set of lines that I am trying to combine into one line starts with the word edit and ends with the word next

At the end of the day, I would like it to run through the file for example file1.txt and parse out the information and push it to another file maybe called file1-parsed.txt with the lines each collapsed down to one line.

Any pointer or tips on what syntax to use to parse like this would be appreciated.

The file looks like this:
config firewall address
edit "device1"
set uuid XYZ123
set subnet 10.2.3.4 255.255.255.255
next
edit "device2"
set uuid XYZ124
set subnet 10.2.3.0 255.255.255.248
next
edit "device3"
set uuid XYZ125
set subnet 10.231.153.32 255.255.255.248
next
end
config firewall address6
edit "device2-v6"
set uuid XYZ126
set ip6 fdff:ffff::/120
next
edit "device3-v6"
set uuid XYZ127
set ip6 fdff:ffff::/120
next
end

I would like to parse through the file and for each edit next combination I would like it to be collapsed into one line as shown below. At the moment I don't care about the lines around it, if I can get the edit next lines combined on to one line each I can work with that. I just don't know the syntax to do this and would appreciate any help. I am starting to watch some python tutorials and I do have knowledge of scripting (have used perl in the past but want to try something new)

config firewall address
edit "device1" set uuid XYZ123 set subnet 10.2.3.4 255.255.255.255 next
edit "device2" set uuid XYZ124 set subnet 10.2.3.0 255.255.255.248 next
edit "device3" set uuid XYZ125 set subnet 10.231.153.32 255.255.255.248 next
end
config firewall address6
edit "device2-v6" set uuid XYZ126 set ip6 fdff:ffff::/120 next
edit "device3-v6" set uuid XYZ127 set ip6 fdff:ffff::/120 next
end

my code here
Reply
#2
To read through the file, use a context manager (with statement) and loop through the file directly:

with log_file as opne('log_test.txt'):
    for line in log_file:
The start method of the string (line will be a string) will allow you to detect the 'edit' and 'next'. I would have a boolean variable and a list of lines to go into the new file. When you hit edit, you switch the boolean to true, after you hit next, switch it to false. If it's true, add the current line to the previous line as one line, when it's false append the new line to the list. Then you can write out the lines to a new file.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020