tsvdel (1185B)
1 #!/bin/sh 2 # vim: set ft=bash: 3 # tsvdel - Delete a line from a TSV file 4 # This program is licensed under the terms of GNU GPL v3 or (at your option) 5 # any later version. Copyright (C) 2023-2026 Страхиња Радић. 6 # See the file LICENSE for exact copyright and license details. 7 8 tab=$(printf "\t") 9 10 usage() 11 { 12 cat <<EOT 13 Usage: ${0##*/} tsvfile.tsv [rowno | colno text] 14 EOT 15 } 16 17 error() 18 { 19 printf "%s: %s\n" "${0##*/}" "$@" >&2 20 } 21 22 case $# in 23 2) inputfile=$1 24 lineno=$2 25 ;; 26 3) inputfile=$1 27 colno=$2 28 searchtext=$3 29 ;; 30 *) usage 31 exit 1 32 ;; 33 esac 34 35 if [ -L $inputfile ]; then 36 inputfile=$(readlink -f $inputfile) 37 fi 38 tmpf=${inputfile}~ 39 40 [ -w "${inputfile}" ] || { error "\`${inputfile}' not writeable"; exit 1; } 41 42 cp "${inputfile}" "$tmpf" 43 # shellcheck disable=SC2064 44 trap "rm -f \"$tmpf\"" HUP PIPE INT QUIT TERM EXIT 45 46 if [ -z "${lineno}" ]; then 47 lineno=$(cut -d"$tab" -f"${colno}" "${inputfile}" | 48 grep -ni "${searchtext}" | 49 sed 's/^\([0-9]\+\).*/\1/;1q') 50 [ -z "$lineno" ] && 51 { error "not found \`${searchtext}' in column ${colno}" 52 exit 1; } 53 fi 54 55 { head -n$((lineno-1)) "${inputfile}" 56 tail +$((lineno+1)) "${inputfile}"; } > "$tmpf" 57 58 mv "$tmpf" "${inputfile}"