fork download
  1. #!/usr/bin/env python3
  2.  
  3. # import re
  4. # import csv
  5. # from datetime import datetime
  6. # import argparse
  7. # import os
  8.  
  9. # # Define the keywords for filtering
  10. # KEYWORD1 = "TIMESTAMP1"
  11. # KEYWORD2 = "TIMESTAMP2"
  12.  
  13. # def extract_timestamp(line):
  14. # timestamp_pattern = r'\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d{6}\+\d{2}'
  15. # match = re.search(timestamp_pattern, line)
  16. # return match.group(0) if match else None
  17.  
  18. # def parse_log_line(line, verbose=False):
  19. # # ... [rest of the function remains unchanged] ...
  20.  
  21. # def process_log(log_file, verbose=False):
  22. # data_dict = {}
  23. # lines_processed = 0
  24. # valid_lines = 0
  25.  
  26. # with open(log_file, 'r') as infile:
  27. # for line in infile:
  28. # lines_processed += 1
  29. # parsed_data = parse_log_line(line, verbose)
  30. # if parsed_data:
  31. # valid_lines += 1
  32. # id_num = parsed_data['Id']
  33. # if id_num not in data_dict:
  34. # data_dict[id_num] = {'Pt': None, 'Et': None, 'start': None, 'sent': None, 'complete': None}
  35.  
  36. # line_type = parsed_data['type']
  37. # data_dict[id_num][line_type] = parsed_data['timestamp']
  38.  
  39. # if line_type == 'start':
  40. # data_dict[id_num]['Pt'] = parsed_data.get('Pt')
  41. # data_dict[id_num]['Et'] = parsed_data.get('Et')
  42.  
  43. # if verbose:
  44. # print("\nProcessing Summary:")
  45. # print(f"Total lines processed: {lines_processed}")
  46. # print(f"Valid lines for data processing: {valid_lines}")
  47. # print(f"Unique Ids found: {len(data_dict)}")
  48.  
  49. # return data_dict
  50.  
  51. # def create_intermediate_log(input_log_file, output_log_file, verbose=False):
  52. # """
  53. # Create an intermediate log file that only contains lines with specific keywords.
  54.  
  55. # Purpose:
  56. # - Filter the input log file based on two predefined keywords.
  57. # - Create a new log file with only the filtered lines.
  58.  
  59. # Expected Behavior:
  60. # - Read the input log file line by line.
  61. # - Check each line for the presence of either KEYWORD1 or KEYWORD2.
  62. # - If a line contains either keyword, write it to the output log file.
  63. # - Provide a summary of the filtering process if verbose mode is enabled.
  64.  
  65. # Args:
  66. # input_log_file (str): Path to the input log file.
  67. # output_log_file (str): Path to the output intermediate log file.
  68. # verbose (bool): Whether to print detailed processing information.
  69.  
  70. # Returns:
  71. # int: The number of lines written to the intermediate log file.
  72. # """
  73. # lines_processed = 0
  74. # lines_written = 0
  75.  
  76. # with open(input_log_file, 'r') as infile, open(output_log_file, 'w') as outfile:
  77. # for line in infile:
  78. # lines_processed += 1
  79. # if KEYWORD1 in line or KEYWORD2 in line:
  80. # outfile.write(line)
  81. # lines_written += 1
  82.  
  83. # if verbose:
  84. # print("\nIntermediate Log Creation Summary:")
  85. # print(f"Input file: {input_log_file}")
  86. # print(f"Output file: {output_log_file}")
  87. # print(f"Total lines processed: {lines_processed}")
  88. # print(f"Lines written to intermediate log: {lines_written}")
  89. # print(f"Filtering keywords: '{KEYWORD1}' and '{KEYWORD2}'")
  90.  
  91. # return lines_written
  92.  
  93. # def calculate_durations(data_dict):
  94. # # ... [function remains unchanged] ...
  95.  
  96. # def write_csv(data_dict, output_file, verbose=False):
  97. # # ... [function remains unchanged] ...
  98.  
  99. # def main(log_file, output_file, verbose=True):
  100. # if verbose:
  101. # print("Starting log processing")
  102. # print(f"Input file: {log_file}")
  103. # print(f"Output file: {output_file}")
  104.  
  105. # # Create intermediate log file name
  106. # base_name = os.path.splitext(output_file)[0]
  107. # intermediate_log_file = f"{base_name}_intermediate.log"
  108.  
  109. # # Create the intermediate log
  110. # lines_filtered = create_intermediate_log(log_file, intermediate_log_file, verbose)
  111.  
  112. # # Process the original log for data extraction
  113. # data_dict = process_log(log_file, verbose)
  114. # calculate_durations(data_dict)
  115. # write_csv(data_dict, output_file, verbose)
  116.  
  117. # print("\nProcessing complete.")
  118. # print(f"Total unique Ids processed: {len(data_dict)}")
  119. # print(f"Output written to: {output_file}")
  120. # print(f"Intermediate log written to: {intermediate_log_file}")
  121. # print(f"Lines in intermediate log: {lines_filtered}")
  122.  
  123. # if __name__ == "__main__":
  124. # parser = argparse.ArgumentParser(description="Parse log file, generate CSV report, and create filtered intermediate log.")
  125. # parser.add_argument("log_file", help="Path to the log file")
  126. # parser.add_argument("-o", "--output", default="output.csv", help="Output CSV file name")
  127. # parser.add_argument("-q", "--quiet", action="store_true", help="Disable verbose mode")
  128. # args = parser.parse_args()
  129.  
  130. # main(args.log_file, args.output, not args.quiet)
  131. print(2)
Success #stdin #stdout 0.03s 9092KB
stdin
Standard input is empty
stdout
2