#!/usr/bin/env bash set -euo pipefail usage() { echo "Использование: $0 --input <файл.csv> [--columns <список>] [--output <файл.txt>] [--no-header] [--force]" } input="" columns="" output="statistics.txt" no_header=0 force=0 while [[ $# -gt 0 ]]; do case "$1" in --input) input="${2:-}"; shift 2 ;; --columns) columns="${2:-}"; shift 2 ;; --output) output="${2:-}"; shift 2 ;; --no-header) no_header=1; shift ;; --force) force=1; shift ;; -h|--help) usage; exit 0 ;; *) echo "Неизвестный аргумент: $1" >&2; usage >&2; exit 2 ;; esac done if [[ -z "$input" ]]; then usage >&2 exit 2 fi for tool in csvstat csvcut; do if ! command -v "$tool" >/dev/null 2>&1; then echo "Не найдена программа: $tool" >&2 exit 2 fi done if [[ ! -f "$input" ]]; then echo "Файл не найден: $input" >&2 exit 2 fi if [[ -e "$output" && "$force" -eq 0 ]]; then echo "Файл уже существует: $output. Добавьте --force" >&2 exit 2 fi mkdir -p "$(dirname "$output")" tmp=$(mktemp "${output}.tmp.XXXXXX") trap 'rm -f "$tmp"' EXIT if [[ -n "$columns" ]]; then selected=$(mktemp "${TMPDIR:-/tmp}/csvstat.XXXXXX") trap 'rm -f "$tmp" "$selected"' EXIT if [[ "$no_header" -eq 1 ]]; then csvcut -H -c "$columns" "$input" > "$selected" else csvcut -c "$columns" "$input" > "$selected" fi csvstat "$selected" > "$tmp" else if [[ "$no_header" -eq 1 ]]; then csvstat --no-header-row "$input" > "$tmp" else csvstat "$input" > "$tmp" fi fi mv "$tmp" "$output" trap - EXIT echo "Колонки: ${columns:-все}" echo "Результат: $output"