Skip to content

Instantly share code, notes, and snippets.

@f9n
Created May 9, 2020 13:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save f9n/cf2f8f8a9edb52c7a21481f6fc5e13d9 to your computer and use it in GitHub Desktop.
Save f9n/cf2f8f8a9edb52c7a21481f6fc5e13d9 to your computer and use it in GitHub Desktop.
Get all ip addresses on file
#!/usr/bin/env bash
# Credits
# https://stackoverflow.com/questions/427979/how-do-you-extract-ip-addresses-from-files-using-a-regex-in-a-linux-shell
file=$1
seperator=${2:-'\n'}
result=$(grep -E -o '(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)' $file)
for ip in $result; do
echo -e "${ip}$seperator\c"
done
echo
### Usage
# $ sezen <file> [seperator]
# $ sezen dirty-file.txt
# 10.250.200.1
# 10.250.200.2
# $ sezen dirty-file.txt ','
# 10.250.200.1,10.250.200.2
@f9n
Copy link
Author

f9n commented May 9, 2020

candan.sh

#!/usr/bin/env bash
file=$1

grep -E -o '(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)' $file

Usage

$ candan dirty-file.txt | xargs printf "%s,"
$ candan dirty-file.txt | xargs printf "%s\n"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment