Find the answer to your Linux question:
Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By gregm
Hello, Hope you can assist me or provide some directions on how to parse the following text: Code: TTL This is another old title CHP1 This is the third item ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Apr 2005
    Location
    Helsinki, Finland
    Posts
    7

    Parsing Text File


    Hello,
    Hope you can assist me or provide some directions on how to parse the following text:

    Code:
    TTL    This is another old title
    CHP1  This is the third item
    CHP2  This is the second item
    TTL    This is another old title
    CHP1  This is the third item
    CHP2  This is the second item
    SCHP  A short sub-chapter
    NEW   This is a length new field, which
            continues on the next line and 
            can be several lines.
    All of this information is in one text file that I need to parse
    Looking forward for your response!

    Merry X-mas All..

    Noel Alex Makumuli

  2. #2
    Linux Engineer
    Join Date
    Dec 2013
    Posts
    1,024
    What programming language/tool are you planning on parsing it with? What is your planned output?

  3. #3
    Just Joined!
    Join Date
    Apr 2005
    Location
    Helsinki, Finland
    Posts
    7
    Apologies for providing partial information.
    Java/ Python are my first preferences.
    Also writing a script for it would be ideal too. Because I have already written a script to split the file using AWK already.

    Regards,
    Noel Alex Makumuli

  4. #4
    Just Joined!
    Join Date
    Apr 2005
    Location
    Helsinki, Finland
    Posts
    7
    Apologies for providing partial information.
    Java/ Python are my first preferences.
    Also writing a script for it would be ideal too. Because I have already written a script to split the file using AWK already. Bash script is my daily tool.

    Regards,
    Noel Alex Makumuli

  5. #5
    Linux Engineer
    Join Date
    Dec 2013
    Posts
    1,024
    I don't use AWK myself but if you already parsing the file with AWK you may get better help by explaining why it isn't doing what you want or expect. I would us python fr this myself. Something along the lines of this could work - depending on what you're trying to accomplish.

    Code:
    !/usr/bin/env python
    
    import re
    
    obj = {}
    current = ''
    
    f = open ('test2.txt', 'r')
    for line in f:
        m = re.match (r"(?P<name>\w+) +(?P<value>.+)", line)
        if m is not None:
            current = m.group('name')
            if current not in obj:
                obj[current] = []
            obj[current].append (m.group('value'))
        else:
            obj[current].append (obj[current].pop() + " " + line.strip())
    
    
    for n, v in obj.iteritems():
        print n, v
    NOEL ALEX MAKUMULI likes this.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •