Strip lines and add the rest of the lines

Hi,

I have a text file which is the running config from a cisco device.

I need to remove the lines before line number 7, strip the lines from 114 till 141 and keep all other lines.

can you please let me know how to do that with python.

I tried like this but not working as expected.

cfg_run = cfg_run.splitlines()
cfg_run = cfg_run[7:,141:114]

Thanks,
Viki

You can create a for-loop or a generator to work yourself through the file and make the changes in the places where you want.

Something like this:
We have an input file log_file.log like this:

1. I'm a test file
2. I'm a test file
3. I'm a test file
4. I'm a test file
5. I'm a test file
6. I'm a test file
7. I'm a test file
8. I'm a test file
9. I'm a test file

Our Python-Script log_changer like this:

with open("log_file.log") as file:
    new_file = open("new_config.log", "w+")
    config_out = ""
    
    for counter, line in enumerate(file, 1):
        if counter < 3:
            pass
        elif counter >= 5 and counter <= 7:
            pass
        else:
            config_out += line

    new_file.write(config_out)

The output will be in new_config.log:

3. I'm a test file
4. I'm a test file
8. I'm a test file
9. I'm a test file

I’m sure there will be more elegant ways but this will help you build your own solution.

Kind regards,
Didier

Hello Didier,

Thanks for the reply. In my situation the contents(running_config of a cisco router) is stored in a variable in the program. To brief more, the running configuration of a file is received from a cisco router using napalm_getters plugin and the content is assigned to a variable. I try to use “with open(“cfg_run”) as file”, in this case it is not working as you explained. I understand your code will work when it is a file.

Here is part of my code to explain what I meant above.

get_run = task.run(task=napalm_get,
getters = [“config”],
retrieve = “running”)
cfg_run = get_run.result[“config”][“running”]

cfg_run = cfg_run.splitlines()

Thanks,
Viki

Hi, so your first line is valid and should work, but I’m curious why you thought this would work:

cfg_run[7:,141:114]

You most likely get this error:

TypeError: list indices must be integers, not tuple

Which means Python just doesn’t do that. I suspect you’re coming from another language that allows this, but you can’t expect every language to be the same. Instead, you have to find out what Python supports and use that. It’s kind of like you are from the UK and drive on the left side of the road. You then traveled to the US and just took your car on the left side of the road and caused an accident. When the police ask you why you go, “Well I mean, that’s what I expected in every country!”

Just like countries, every programming language is different with some similarities due to history. In this case you could just invert the problem:

front = cfg_run[8:113]
back = cfg_run[142:]

If you have to remove lines, then you don’t need to do exactly what the problem says and try to remove them. You can instead do the inverse and accept the lines you want.

You should now go read about Python list index range syntax.

1 Like

Hey Zed,

Thanks for the reply.

I did the following and it worked.

cfg_run = cfg_run.splitlines()
cfg_run = cfg_run[7:41] + cfg_run[50:166] + cfg_run[178:198]
cfg_run = “\n”.join(cfg_run)

Thanks for your help.

Regards,
Viki

2 Likes