ambevar-dotfiles/.scripts/prepend

20 lines
326 B
Bash
Executable File

#!/bin/sh
if [ $# -eq 0 ] || [ "$1" = "-h" ]; then
cat <<EOF
Usage: ${0##*/} STRING [FILES]
Prepend STRING to FILES or stdin if unspecified. STRING may not contain pipes
('|').
EOF
exit
fi
VAR="$1"
shift
[ $# -eq 0 ] && OPT_INPLACE="" || OPT_INPLACE="-i"
sed -n $OPT_INPLACE '1{s|^|'$VAR'\n|;h};1!H;${x;p}' "$@"