Skip to content

Instantly share code, notes, and snippets.

@FranklyFuzzy
Created April 15, 2024 20:55
Show Gist options
  • Save FranklyFuzzy/fc5a0cb2487ce8d8cd336a2c3698d0e6 to your computer and use it in GitHub Desktop.
Save FranklyFuzzy/fc5a0cb2487ce8d8cd336a2c3698d0e6 to your computer and use it in GitHub Desktop.
Extract IPs from file
#!/bin/bash
# Check if a filename is provided as an argument
if [ $# -ne 1 ]; then
echo "Usage: $0 <filename>"
exit 1
fi
filename="$1"
# Read the file line by line
while read -r line; do
# Modify the regex pattern as needed for your specific use case
ip_regex='([0-9]{1,3}\.){3}[0-9]{1,3}'
# Extract IPs from the line
while [[ "$line" =~ $ip_regex ]]; do
ip="${BASH_REMATCH[0]}"
#echo "Found IP: $ip"
echo $ip
line="${line/${BASH_REMATCH[0]}/}"
done
done < "$filename"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment