Extract Software version from Cisco Devices

#!/bin/bash

input_file="input.txt"
output_file="output.txt"

while IFS= read -r line; do
    # Extract text before the first colon
    name=$(echo "$line" | sed -n 's/^\([^:]*\):.*/\1/p')

    # Match version patterns like 10.3(1), 10.4(3a), 7.3(9)D1(1), etc.
    version=$(echo "$line" | grep -oP '\b\d{1,2}(\.\d{1,2})*(\([^)]+\))?([A-Z]?\d*(\([^)]+\))*)*')

    if [[ -n "$name" && -n "$version" ]]; then
        echo "$name,$version"
    fi
done < "$input_file" > "$output_file"
  • Explanation:

    • \b: Word boundary (ensures match starts cleanly at a digit, not mid-word)

    • \d{1,2}: Matches the first 1- or 2-digit number (e.g. 10, 7)

    • (.\d{1,2})*: Matches zero or more groups of a dot + 1–2 digit number (e.g. .3, .4)

    • (([^)]+))?: Optionally matches a first parenthetical block (e.g. (1), (3a))

    • ([A-Z]?\d*(([^)]+))): Matches optional trailing letters, digits, or nested parentheses (e.g. D1(1), D22(5a))

Last updated