Created
November 9, 2015 09:23
-
-
Save eirenik0/0a1e2b70885892aba4d1 to your computer and use it in GitHub Desktop.
Поиск по ключевому слову по всем логам с помощью sed
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
#!/usr/bin/env bash | |
set -e | |
ME=`basename $0` | |
function print_help() { | |
echo "Поиск по ключевому слову по всем логам с помощью sed" | |
echo | |
echo "Использование: $ME options..." | |
echo "Параметры:" | |
echo " -s text Искать указанное слово." | |
echo " -w text Запись в файл найденые строки." | |
echo " -h Справка." | |
echo | |
} | |
function search_in_logs() { | |
strings /var/log/journal/6a2bfa34fd014e1ba23d8d8c5d69a39f/system.journal > tmp_logs | |
SED_OUTPUT=$(sed "/$SEARCH_WORD/!d" tmp_logs) | |
echo "Искомое слово: $SEARCH_WORD\n$SED_OUTPUT" | |
rm tmp_logs | |
} | |
function write_to_file { | |
echo "$SED_OUTPUT" >> $FILE_NAME | |
} | |
# Если скрипт запущен без аргументов, открываем справку. | |
if [ $# = 0 ]; then | |
print_help | |
fi | |
while getopts ":s:w:r" opt ; | |
do | |
case $opt in | |
s) SEARCH_WORD=$OPTARG; | |
search_in_logs | |
;; | |
w) FILE_NAME=$OPTARG; | |
write_to_file | |
;; | |
*) echo "Неправильный параметр"; | |
echo "Для вызова справки запустите $ME -h"; | |
exit 1 | |
;; | |
esac | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment