tsvfind (1218B)
1 #!/bin/sh 2 # vim: set ft=bash: 3 # tsvfind - Search for text in 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 nl=' 10 ' 11 12 usage() 13 { 14 cat <<EOT 15 Usage: ${0##*/} [-s rowno] tsvfile.tsv [text | colno text] 16 -s Start at lineno 17 EOT 18 } 19 20 error() 21 { 22 printf "%s: %s\n" "${0##*/}" "$@" >&2 23 } 24 25 rowno=0 26 27 OPTIND=1 28 while getopts s: OPT >/dev/null 2>&1 29 do 30 case $OPT in 31 s) rowno="$OPTARG" 32 ;; 33 ?) error "Unknown parameter '$1'" 34 usage >&2 35 exit 1 36 ;; 37 esac 38 OPTARG= 39 done 40 shift $((OPTIND-1)) 41 42 case $# in 43 2) inputfile=$1 44 searchtext=$2 45 ;; 46 3) inputfile=$1 47 colno=$2 48 searchtext=$3 49 ;; 50 *) usage 51 exit 1 52 ;; 53 esac 54 55 if [ -n "$colno" ]; then 56 awk 'BEGIN { FS = "\t"; RS = "\n" } 57 { 58 if (NR > '"$rowno"' && !found && 59 $'"$colno"' ~ ".*'"$searchtext"'.*") 60 { 61 print NR 62 found = 1 63 exit 0 64 } 65 } 66 END { exit !found }' "$inputfile" 67 else 68 awk 'BEGIN { FS = "\t"; RS = "\n" } 69 { 70 if (NR > '"$rowno"' && !found && 71 $0 ~ ".*'"$searchtext"'.*") 72 { 73 print NR 74 found = 1 75 exit 0 76 } 77 } 78 END { exit !found }' "$inputfile" 79 fi