#!/bin/bash

show_help() {
cat <<EOF
Использование: $0 -f input_file "слово1 слово2 ..."

Ищет секции в файле (каждая начинается строкой [...]),
выводит только те, где встречаются все указанные слова как полные слова (без учёта регистра).

EOF
}

while getopts ":f:h" opt; do
case $opt in
f) input_file="$OPTARG" ;;
h) show_help; exit 0 ;;
\?) echo "Неизвестная опция -$OPTARG" >&2; show_help; exit 1 ;;
:) echo "Опция -$OPTARG требует аргумент" >&2; show_help; exit 1 ;;
esac
done

shift $((OPTIND-1))

search_words="$1"

if [[ -z "$input_file" || -z "$search_words" ]]; then
echo "Ошибка: укажите входной файл (-f) и слова для поиска."
show_help
exit 1
fi

if [[ ! -f "$input_file" ]]; then
echo "Ошибка: файл '$input_file' не найден."
exit 2
fi

awk -v words="$search_words" '
function check_words_in_section(section, words_array, len, i, word, pattern) {
section_l = tolower(section)
for (i = 1; i <= len; i++) {
word = tolower(words_array[i])
pattern = "(^|[^[:alnum:]_])" word "([^[:alnum:]_]|$)"
if (section_l !~ pattern) return 0
}
return 1
}

BEGIN {
len = split(words, search_words, " ")
section = ""
in_section = 0
}

/^\[.*\]$/ {
if (in_section && check_words_in_section(section, search_words, len)) print section
section = $0 "\n"
in_section = 1
next
}

{
if (in_section) section = section $0 "\n"
}

END {
if (in_section && check_words_in_section(section, search_words, len)) print section
}
' "$input_file"

[file-name 000177_2025-07-25_19-02-35.txt]