From d985756c9a1ea7096c1cd24ac01caa54c6a1122d Mon Sep 17 00:00:00 2001 From: Chan Wei Nick Date: Tue, 25 Mar 2025 09:21:39 +0800 Subject: [PATCH] push compareHostname --- .gitignore | 5 +++++ README.md | 5 +++++ compareHostname.py | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 46 insertions(+) create mode 100644 compareHostname.py diff --git a/.gitignore b/.gitignore index 0a19790..901c25b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,8 @@ +# Nick self-add + +# sensitive files that could not be shared +rawFiles/ + # Byte-compiled / optimized / DLL files __pycache__/ *.py[cod] diff --git a/README.md b/README.md index 0a006e2..1f8ed25 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,7 @@ # SW_PythonScripts + Random Scripts for Making Life Easier + +## compareHostname.py + +Allows to compare between 2 text files, 'hostname="{...}"', and print out unique hostnames for easier management \ No newline at end of file diff --git a/compareHostname.py b/compareHostname.py new file mode 100644 index 0000000..e234a40 --- /dev/null +++ b/compareHostname.py @@ -0,0 +1,36 @@ +import re + +def extract_hostnames(file_path): + hostname_pattern = r'hostname="([^"]+)"' + hostnames = set() # Use a set to avoid duplicates within the same file + + try: + with open(file_path, 'r', encoding='utf-8') as file: + for line in file: + matches = re.findall(hostname_pattern, line) + hostnames.update(matches) + except FileNotFoundError: + print(f"File not found: {file_path}") + except Exception as e: + print(f"An error occurred: {e}") + + return hostnames + +if __name__ == "__main__": + file1 = "rawFiles/file1.txt" + file2 = "rawFiles/file2.txt" + + hostnames_file1 = extract_hostnames(file1) + hostnames_file2 = extract_hostnames(file2) + + # Find unduplicated hostnames + unique_to_file1 = hostnames_file1 - hostnames_file2 + unique_to_file2 = hostnames_file2 - hostnames_file1 + + print("Hostnames unique to file1:") + for hostname in unique_to_file1: + print(hostname) + + print("\nHostnames unique to file2:") + for hostname in unique_to_file2: + print(hostname) \ No newline at end of file