CSV to Address External Datagroup File
Published Mar 18, 2023
Version 1.0Was this article helpful?
Thanks Jason! You always seem to find a smart fix for an annoying problem 🙂
For the minority which doesn't master Python yet (like me 😋) I have concocted a Bash version of it:
#!/bin/bash
# read IP addresses from CSV file into array
mapfile -t ipaddrs < dg_ipaddrs.csv
# loop through array and format each IP address
for addr in "${ipaddrs[@]}"; do
# remove any leading/trailing whitespace
addr="$(echo -e "${addr}" | tr -d '[:space:]')"
# split line into fields on comma delimiter
IFS=',' read -ra fields <<< "$addr"
# check if first field contains a network mask or prefix length. The ${fields[0],,} syntax is used to convert the contents of the fields[0] variable to lowercase.
if [[ "${fields[0]}" == */* ]] || [[ "${fields[0],,}" == *"mask"* ]] || [[ "${fields[0],,}" == *"prefixlen"* ]]; then
# check if second field is empty
if [[ -z "${fields[1]}" ]]; then
echo "network ${fields[0]},"
else
value="${fields[1]}"
echo "network ${fields[0]} := ${value},"
fi
else
# check if second field is empty
if [[ -z "${fields[1]}" ]]; then
echo "host ${fields[0]},"
else
value="${fields[1]}"
echo "host ${fields[0]} := ${value},"
fi
fi
done > dg_formatted.txt