#!/bin/bash
# Ilkay Alakay <ilkay@astronix.org>. Licensed under The Unlicense
[ $# -ne 3 ] && { echo "usage: ipatch <file> <line> <new line>"; exit 1; }
f=$1 n=$2 new=$3
[ -f "$f" ] || { echo "unable to find a file named $f"; exit 1; }
[[ $n =~ ^[0-9]+$ ]] || { echo "<line> should be a number"; exit 1; }
total=$(wc -l < "$f")
(( n >= 1 && n <= total )) || { echo "<line> is out of range (1-$total)"; exit 1; }
tmp=$(mktemp) || exit 1
NEW=$new awk -v n="$n" 'NR==n{print ENVIRON["NEW"];next}1' "$f" > "$tmp" && cat "$tmp" > "$f"
rm -f "$tmp"
