Created
July 10, 2020 07:57
-
-
Save vedmichv/b174d30a1a6335d53b748b79a5ae7685 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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[@]} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Solution