#!/usr/bin/env bash set -euo pipefail usage() { echo "Использование: $0 <входной файл> [выходной файл] [--in-place]" } input="" output="" in_place=0 while [[ $# -gt 0 ]]; do case "$1" in --in-place) in_place=1; shift ;; -h|--help) usage; exit 0 ;; --*) echo "Неизвестный флаг: $1" >&2; usage >&2; exit 2 ;; *) if [[ -z "$input" ]]; then input="$1" elif [[ -z "$output" ]]; then output="$1" else echo "Лишний аргумент: $1" >&2 exit 2 fi shift ;; esac done if [[ -z "$input" || ! -f "$input" ]]; then echo "Укажите существующий входной файл" >&2 exit 2 fi if [[ "$in_place" -eq 1 && -n "$output" ]]; then echo "С --in-place выходной файл указывать нельзя" >&2 exit 2 fi if [[ -n "$output" && "$output" == "$input" ]]; then echo "Выходной файл совпадает с входным, используйте --in-place" >&2 exit 2 fi removed="$(awk 'NF == 0 { count++ } END { print count + 0 }' "$input")" if [[ "$in_place" -eq 1 ]]; then backup="${input}.bak" cp -p -- "$input" "$backup" tmp="$(mktemp "${input}.XXXXXX")" trap 'rm -f -- "$tmp"' EXIT awk 'NF > 0' "$input" > "$tmp" mv -- "$tmp" "$input" trap - EXIT echo "Удалено строк: $removed, копия: $backup" elif [[ -n "$output" ]]; then awk 'NF > 0' "$input" > "$output" echo "Удалено строк: $removed, результат: $output" else awk 'NF > 0' "$input" fi