String manipulation by specific delimiter and write in text file

Ahmed Ayman Ibrahim

I am writing a function that takes a file updates.txt as an input. The file looks like this :

---------------------------------------------------
MRT Header
    Timestamp: 1453939200(2016-01-28 01:00:00)
    Type: 16(BGP4MP)
    Subtype: 4(BGP4MP_MESSAGE_AS4)
    Length: 39
BGP4MP_MESSAGE_AS4
    Peer AS Number: 37989
    Local AS Number: 12654
    Interface Index: 0
    Address Family: 1(IPv4)
    Peer IP Address: 203.123.48.6
    Local IP Address: 193.0.4.28
BGP Message
    Marker: -- ignored --
    Length: 19
    Type: 4(KEEPALIVE)
---------------------------------------------------
MRT Header
    Timestamp: 1453939200(2016-01-28 01:00:00)
    Type: 16(BGP4MP)
    Subtype: 4(BGP4MP_MESSAGE_AS4)
    Length: 118
BGP4MP_MESSAGE_AS4
    Peer AS Number: 1836
    Local AS Number: 12654
    Interface Index: 0
    Address Family: 1(IPv4)
    Peer IP Address: 146.228.1.3
    Local IP Address: 193.0.4.28
BGP Message
    Marker: -- ignored --
    Length: 98
    Type: 2(UPDATE)
    Withdrawn Routes Length: 0
    Total Path Attribute Length: 71
    Path Attribute Flags/Type/Length: 0x40/1/1
        ORIGIN: 0(IGP)
    Path Attribute Flags/Type/Length: 0x40/2/42
        AS_PATH
            Path Segment Type: 2(AS_SEQUENCE)
            Path Segment Length: 10
            Path Segment Value: 1836 174 6453 37282 37088 37629 37629 37629 37629 37629
    Path Attribute Flags/Type/Length: 0x40/3/4
        NEXT_HOP: 146.228.1.3
    Path Attribute Flags/Type/Length: 0xc0/8/12
        COMMUNITY: 1836:110 1836:6000 1836:6031
    NLRI: 154.65.7.0/24
---------------------------------------------------

The file is a sequence of 'blocks'. Each block is enclosed between the line of dashes

---------------------------------------------------
# Block (n)
---------------------------------------------------
# Block (n+1)
---------------------------------------------------
# Block (n+2) , etc

I would like to read the whole file, block-by-block, and return a text file only containing the lines of the fields : Timestamp, Peer AS number, Local AS number, Peer IP Address, Local IP Address.

The resulting .txt file should look something like this :

---------------------------------------------------
MRT Header
    Timestamp: 1453939200(2016-01-28 01:00:00)
BGP4MP_MESSAGE_AS4
    Peer AS Number: 37989
    Local AS Number: 12654
    Peer IP Address: 203.123.48.6
    Local IP Address: 193.0.4.28
---------------------------------------------------
MRT Header
    Timestamp: 1453939200(2016-01-28 01:00:00)
BGP4MP_MESSAGE_AS4
    Peer AS Number: 1836
    Local AS Number: 12654
    Peer IP Address: 203.123.48.6
    Local IP Address: 193.0.4.28
---------------------------------------------------

Ideally, I want to overwrite the updates.txt with the new text file not to waste space, and save it in a new directory "Parsed Updates".

I know it is minimal as I am stuck with the line of dashes delimiter, but my code looks like this :

import sys
import os

def parser(filename):
    info = open(filename, 'r+')
    info.read()

    #Here comes the string manipulation code
    #info.split( '---------------------------------------------------')

    info.close()
    print 'The file has been parsed successfully !!'

def main():
    parser('updates.txt')


if __name__=='__main__':
    main()
Shan Valleru
>>> with open('results.txt', 'wb') as r:
...     with open('updates.txt', 'rb') as u:
...         for line in u.readlines():
...             if '-'*51 in line:
...                 r.write(line)
...             else:
...                 if any(field in line for field in ['Timestamp', 'Peer AS Number', 'Local AS Number', 'Peer IP Address', 'Local IP Address','MRTHeader']):
...                     r.write(line)

your results file will look this:

$ cat results.txt
---------------------------------------------------
MRT Header
    Timestamp: 1453939200(2016-01-28 01:00:00)
    Peer AS Number: 37989
    Local AS Number: 12654
    Peer IP Address: 203.123.48.6
    Local IP Address: 193.0.4.28
---------------------------------------------------
MRT Header
    Timestamp: 1453939200(2016-01-28 01:00:00)
    Peer AS Number: 1836
    Local AS Number: 12654
    Peer IP Address: 146.228.1.3
    Local IP Address: 193.0.4.28
---------------------------------------------------

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to write a text file with no any delimiter in python?

Fastest way to read / write text file, excluding specific string

Write String to a text file

How to write to a file using comma delimiter and no double quote in string?

How to write String to a text File

Delimiter with tab in text file

Using delimiter on a text file

Intricate file/text manipulation

Text file manipulation in perl

How to write to a text file in a specific format

How to write a string at a specific line of a plain text file using Racket language?

Can I use Powershell to automatically extract an unknown string with a specific pattern from a XML file and write that string in a text file?

Write a string to a specific position in a file in python

Read and write a String from text file

Read text file into string array (and write)

How to write specific text from text file to textbox?

Read text file and write file from specific contents

Parsing text file with a complicated delimiter

Importing text file with length delimiter

Parsing a text file with a tab delimiter

Text File Manipulation using For Loop

Delete lines in a text file that contain a specific string with /

Splitting text file by specific string/keyword

How to replace specific String in a text file by java?

Count specific string in text file using PowerShell

Append string in a specific place of a text file

Reading a specific part of a text file as a string in C?

Delphi ListBox to a Text file with a specific string

How to append string somewhere specific in text file