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 6066113
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:27:57+00:00 2026-05-23T09:27:57+00:00

Please can someone help me solve the following query? I have a log file

  • 0

Please can someone help me solve the following query?
I have a log file with thousands of lines like the following:-

    jarid: 7e5ae720-9151-11e0-eff2-00238bce4216 recv: 1 timestamp: 00:00:02,217
    jarid: 7e5ae720-9151-11e0-eff2-00238bce4216 ack: 13 timestamp: 00:00:04,537
    jarid: 462c6d11-9151-11e0-a72c-00238bbdc9e7 recv: 1 timestamp: 00:00:08,018
    jarid: 462c6d11-9151-11e0-a72c-00238bbdc9e7 nack: 14 timestamp: 00:00:10,338

I would like to write a python script to iterate through this file and based on the jarid (the second field in the log file) to get the timestamp from each line where the jarid is found and print them on the same line. So for example, for the following two lines:-

    jarid: 7e5ae720-9151-11e0-eff2-00238bce4216 recv: 1 timestamp: 00:00:02,217 
    jarid: 7e5ae720-9151-11e0-eff2-00238bce4216 ack: 13 timestamp: 00:00:04,537

I would get the following output:-

    jarid: 7e5ae720-9151-11e0-eff2-00238bce4216 recv: 00:00:02,217 ack: 00:00:04,537

I think the best way to accomplish this is with a dictionary (or maybe not!, please comment). I have written the following script, which is somewhat working, but it is not giving me the desired output:-

#!/opt/SP/bin/python

    log = file(/opt/SP/logs/generic.log, "r")
    filecontent = log.xreadlines()
    storage = {}
    for line in filecontent:
        line = line.strip()
        jarid, JARID, status, STATUS, timestamp, TIME = line.split(" ")
        if JARID not in storage:
            storage[JARID] = {}
        if STATUS not in storage[JARID]:
            storage[JARID][STATUS] = {}
        if TIME not in storage[JARID][STATUS]:
            storage[JARID][STATUS][TIME] = {}

    jarids = storage.keys()
    jarids.sort()
    for JARID in jarids:
        stats = storage[JARID].keys()
        stats.sort()
        for STATUS in stats:
            times = storage[JARID][STATUS].keys()
            times.sort()
            for TIME in times:
                all = storage[JARID][STATUS][TIME].keys()
                all.sort()

    for JARID in jarids:
        if "1" in storage[JARID].keys() and "13" in storage[JARID].keys():
            print "MSG: %s, RECV: %s, ACK: %s" % (JARID, storage[JARID]["1"], storage[JARID]["13"])
        else:
            if "1" in storage[JARID].keys() and "14" in storage[JARID].keys():
                print "MSG: %s, RECV: %s, NACK: %s" % (JARID, storage[JARID]["1"], storage[JARID]["14"])

When I run this script, I am getting the following output:-

    MSG: 7e5ae720-9151-11e0-eff2-00238bce4216, RECV: {'00:00:02,217': {}}, ACK: {'00:00:04,537': {}}

Please note that I am still learning python and that my scripting skills are not all that!

Please, can you help me figure out how to get the desired output as I wrote above?

  • 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-23T09:27:57+00:00Added an answer on May 23, 2026 at 9:27 am

    That should work. Updated.

    using:

    log = ['jarid: 7e5ae720-9151-11e0-eff2-00238bce4216 recv: 1 timestamp: 00:00:02,217',
           'jarid: 7e5ae720-9151-11e0-eff2-00238bce4216 ack: 13 timestamp: 00:00:04,537',
           'jarid: 462c6d11-9151-11e0-a72c-00238bbdc9e7 recv: 1 timestamp: 00:00:08,018',
           'jarid: 462c6d11-9151-11e0-a72c-00238bbdc9e7 nack: 14 timestamp: 00:00:10,338']
    

    you can do:

    d = {}
    for i in (line.split() for line in log):
        d.setdefault(i[1], {}).update({i[2]:i[-1]})
    
    #as pointed by @gnibbler, you can also use "defaultdict"
    #instead of dict with "setdefault"
    

    then you may print it with:

    for i,j in d.items():
        print 'jarid:', i,
        for k,m in j.items():
            print k, m,
        print
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please can someone help? I have the following code which uploads a file to
Can someone please help me figure out a way to achieve the following (see
I hope someone can help me with the following... I have this code below
Can someone please help me out with printing the contents of an IFrame via
Can someone please help me? In Perl, what is the difference between: exec command;
Can someone please help quickly mute or unmute the stage volume in Flash CS3
can someone please help me. why does this return an error: Dim stuff As
Can someone please help me with using Regex with NSPredicate? NSString *regex = @(?:[A-Za-z0-9]);
Can someone please help me out with a JavaScript/jQuery solution for this arithmetic problem:
Could someone please help explain why I can't get this to work? I properly

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.