Skip to main content

chat1

import argparse

def process_logs(filepath, line_delimiter, column_number):
    records = []
    current_record = ""
    
    try:
        with open(filepath, 'r') as file:
            for line in file:
                parts = line.strip().split(';')
                if len(parts) > column_number:
                    target_column = parts[column_number - 1]
                    
                    # If delimiter is found, start a new record
                    if line_delimiter in target_column:
                        if current_record:
                            records.append(current_record.strip())
                        current_record = target_column
                    else:
                        # Continue adding to the current record
                        if current_record:
                            current_record += " " + target_column
                        else:
                            current_record = target_column
    
        # Add the last record if it exists
        if current_record:
            records.append(current_record.strip())
    
    except FileNotFoundError:
        print(f"Error: The file '{filepath}' does not exist.")
        return []
    except Exception as e:
        print(f"An error occurred: {e}")
        return []

    return records

def main():
    parser = argparse.ArgumentParser(description='Process logs based on a delimiter and column number.')
    parser.add_argument('filepath', type=str, help='Path to the log file')
    parser.add_argument('line_delimiter', type=str, help='Delimiter to indicate the start of a new record')
    parser.add_argument('column_number', type=int, help='Column number to search for the delimiter')

    args = parser.parse_args()

    results = process_logs(args.filepath, args.line_delimiter, args.column_number)
    for result in results:
        print(result)

if __name__ == "__main__":
    main()