💾 Archived View for alpha.lyk.so › systems › bins › scripts › add.sh captured on 2024-07-08 at 23:48:59.

View Raw

More Information

⬅️ Previous capture (2023-01-29)

-=-=-=-=-=-=-

#!/usr/bin/env bash

set -e

[ "$1" ] || { >&2 echo "usage: $0 <tsv file>"; exit 1; }

[ -f "$1" ] || { >&2 echo "no such file: $1"; exit 1; }

prefix="$(tail -n1 "$1" | cut -f1 | grep -o "[A-Z ]\+")"
last_id="$(tail -n1 "$1" | cut -f1 | grep -o "[0-9]\+")"
next_id="$(expr "$last_id" + 1)"
new_line="$prefix$next_id"

while read -u 3 heading; do
	if [ "$heading" = "ID" ]; then
		printf "%s: %s\n" "$heading" "$prefix$next_id"
	else
		printf "%s: " "$heading"
		read value
		new_line="$(printf "$new_line\t$value")"
	fi

done 3< <(head -n1 "$1" | tr '\t' '\n')

echo "$new_line" >> "$1"