Files
komp_ac/prod_data_parse_scripts/parse_uctovnictvo.sh
2026-09-03 19:21:54 +02:00

89 lines
1.5 KiB
Bash
Executable File

#!/bin/sh
set -eu
if [ "$#" -lt 1 ] || [ "$#" -gt 2 ]; then
printf 'Usage: %s INPUT.csv [OUTPUT.csv]\n' "$0" >&2
exit 2
fi
input=$1
output=${2:--}
process_csv() {
awk -F '\",\"' '
function clean_account(value) {
if (length(value) > 0 && substr(value, length(value), 1) !~ /[0-9]/) {
while (length(value) > 0 && substr(value, length(value), 1) !~ /[0-9]/) {
value = substr(value, 1, length(value) - 1)
}
}
return value
}
function zero_amount_to_empty(value) {
if (value == "0.00") {
return ""
}
return value
}
BEGIN {
OFS = FS
}
{
sub(/\r$/, "", $0)
}
NR == 1 {
print
next
}
{
if ($6 == "- -") {
$6 = ""
}
cleaned = clean_account($10)
if (cleaned == "") {
cleaned = "000"
}
key = cleaned
if (key == "000") {
next
}
key_count[key]++
if (!(key in key_order)) {
key_order[key] = ++key_total
}
$10 = cleaned
$11 = zero_amount_to_empty($11)
$12 = zero_amount_to_empty($12)
print
}
END {
for (key in key_order) {
if (key_count[key] > 1) {
printf "Non-unique column 10 key: %s (%d rows)\n", key, key_count[key] > "/dev/stderr"
}
}
}
' "$input"
}
if [ "$output" = "-" ]; then
process_csv
else
process_csv > "$output"
fi