Skip to content

Instantly share code, notes, and snippets.

@vedmichv
Created July 10, 2020 07:57
Show Gist options
  • Save vedmichv/b174d30a1a6335d53b748b79a5ae7685 to your computer and use it in GitHub Desktop.
Save vedmichv/b174d30a1a6335d53b748b79a5ae7685 to your computer and use it in GitHub Desktop.
#!/bin/bash
#
#
# есть большой файл логов с кучей строк типа:
# 10.10.2020 1.2.3.4 /index.html
# нужно найти последние уникальные 100 IP адресов в файлике
#
#
result=()
n=10
while read line; do
ip=$(echo $line | awk --posix '$2 ~ /^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}/' | awk '{print $2}')
if [[ ! " ${result[@]} " =~ " ${ip} " ]]; then
result+=(${ip})
fi
if [ ${#result[@]} -ge $n ]; then
break
fi
done < <(sed '1!G;h;$!d' a.log)
printf '%s\n' "${result[@]}"
echo ${result[@]}
@vedmichv
Copy link
Author

Solution

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