Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3938604
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T00:12:13+00:00 2026-05-20T00:12:13+00:00

I need to parse a large log file (flat file), which contains two column

  • 0

I need to parse a large log file (flat file), which contains two column of values (column-A , column-B).

Values in both columns are repeating. I need to find for each unique value in column-A , I need to find a set of column-B values.

Is this can be done using unix shell command or need to write any perl or python script? What are the ways this can be done?

Example:

xxxA 2
xxxA 1
xxxB 2
XXXC 3
XXXA 3
xxxD 4

output:

xxxA - 2,1,3
xxxB - 2
xxxC - 3
xxxD - 4
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T00:12:13+00:00Added an answer on May 20, 2026 at 12:12 am

    I would use Python dictionaries where the dictionary keys are column A values and the dictionary values are Python’s built-in Set type holding column B values

    def parse_the_file():
        lower = str.lower
        split = str.split
        with open('f.txt') as f:
            d = {}
            lines = f.read().split('\n')
            for A,B in [split(l) for l in lines]:
                try:
                    d[lower(A)].add(B)
                except KeyError:
                    d[lower(A)] = set(B)
    
            for a in d:
                print "%s - %s" % (a,",".join(list(d[a])))
    
    if __name__ == "__main__":
        parse_the_file()
    

    The advantage of using a dictionary is that you’ll have a single dictionary key per column A value. The advantage of using a set is that you’ll have a unique set of column B values.

    Efficiency notes:

    • The use of try-catch is more efficient than using an if\else statement to check for initial cases.
    • The evaluation and assignment of the str functions outside of the loop is more efficient than simply using them inside the loop.
    • Depending on the proportion of new A values vs. reappearance of A values throughout the file, you may consider using a = lower(A) before the try catch statement
    • I used a function, as accessing local variables is more efficient in Python than accessing global variables
    • Some of these performance tips are from here

    Testing the code above on your input example yields:

    xxxd - 4
    xxxa - 1,3,2
    xxxb - 2
    xxxc - 3
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to parse a xml file which is practically an image of a
I need to parse very large log files (>1Gb, <5Gb) - actually I need
I have a few very large log files, and I need to parse them.
I have a large server log file (~750 MB) which I can't open with
I need to parse a fairly large XML file (varying between about a hundred
I have a collection of HTML documents for which I need to parse the
I have a log file that continually logs short lines. I need to develop
I have a problem which requires me to parse several log files from a
I have a large amount of Word documents that I need to parse. As
I have a very large JSON string that I need to parse with in-browser

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.