Thanks to visit codestin.com
Credit goes to github.com

Skip to content

How to extract the most recent timestamp of data #583

Open
@jiuyuan-light

Description

@jiuyuan-light

Hello everyone, I have the following data (in the code) and I would like to extract the closest timestamp to the data, such as "Tue Nov 12 02:30:51 2000". However, the result I obtained is not what I wanted. What should I do, thank you.

`['Tue Nov 12 01:30:51 2000', 'cmd_prefix a\n cmd output1\n']

  • DATA: ['cmd_prefix a\n cmd output1\n']
  • TIMESTAMP: ['Tue Nov 12 01:30:51 2000']`

data = """
[Tue Nov 12 01:30:51 2000]
abcd
[Tue Nov 12 02:30:51 2000]
1234
cmd_prefix a
    cmd output1
cmd_prefix b
[Tue Nov 12 03:30:51 2000]
abcd
[Tue Nov 12 04:30:51 2000]
1234
cmd_prefix a
    cmd output2
cmd_prefix b
"""

timestamp = (
    LineStart()
    + Suppress(Optional(Literal("[")))
    + Regex(r"\w{3} \w{3}\s+\d{1,2} \d{2}:\d{2}:\d{2} \d{4}")
    + Suppress(Optional(Literal("]")))
)
cmd_prefix_cmd = LineStart() + Literal("cmd_prefix")
cmd_prefix_m_cmd = Combine(cmd_prefix_cmd + Literal(" a"))
cmd_prefix_z_cmd = Combine(cmd_prefix_cmd + Literal(" b"))
cmd_prefix_parse = Suppress(SkipTo(cmd_prefix_m_cmd)) + SkipTo(cmd_prefix_z_cmd)

timestamp_with_data = Group(
    OneOrMore(timestamp)("TIMESTAMP")
    + FollowedBy(cmd_prefix_parse)
    + cmd_prefix_parse("DATA")
)

parser = OneOrMore(timestamp_with_data)

results = parser.parseString(data)

for result in results:
    print(result.dump())

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions