чување 5a39ea4c67ff22a6e0b713c52d763b73ae6cfa54
родитељ 1a36c6ff5bc6c255a164150ecc5bdab7b78ac8ed
Аутор: Страхиња Радић <contact@strahinja.org>
Датум: Sun, 3 Sep 2023 02:01:34 +0200
tsvmove,tsvmove.1.in,test/test.tsv: New files; Optimize, clarify license notices
Signed-off-by: Страхиња Радић <contact@strahinja.org>
Diffstat:
измењених датотека: 11, додавања: 154(+), брисања: 32(-)
diff --git a/TODO b/TODO
@@ -1,8 +1,10 @@
TODO
====
-[ ] Commands to move the current row up/down by [n] rows and/or to exact row
+[~] Commands to move the current row up/down by [n] rows and/or to exact row
number
+ [x] to exact row number
+ [ ] by [n]
[ ] Handle large tables
[ ] Add `lines` command similar to `cols`
diff --git a/all.do b/all.do
@@ -1 +1,2 @@
-redo-ifchange ste ste.1.gz transpose.1.gz tsvedit.1.gz tsvdel.1.gz tsvins.1.gz
+redo-ifchange ste ste.1.gz transpose.1.gz tsvedit.1.gz tsvdel.1.gz tsvins.1.gz \
+ tsvmove.1.gz
diff --git a/test/test.tsv b/test/test.tsv
@@ -0,0 +1,5 @@
+ID Name Age
+1 Peter 124
+2 John 12
+3 Stephen 33
+4 Kevin 52
diff --git a/tsvdel b/tsvdel
@@ -1,12 +1,14 @@
#!/bin/sh
# vim: set ft=bash:
# tsvdel - Delete a line from a TSV file
-# See the file LICENSE for copyright and license details.
+# This program is licensed under the terms of GNU GPL v3 or (at your option)
+# any later version. Copyright (C) 2023 Страхиња Радић.
+# See the file LICENSE for exact copyright and license details.
usage()
{
cat <<EOT
-Usage: ${0##*/} [tsvfile.tsv] [[rowno] | [colno] [text]]
+Usage: ${0##*/} tsvfile.tsv [rowno | colno text]
EOT
}
@@ -45,7 +47,7 @@ if [ -z "${lineno}" ]; then
exit 1; }
fi
-head -n$((lineno-1)) "${inputfile}" > "$tmpf"
-tail +$((lineno+1)) "${inputfile}" >> "$tmpf"
+{ head -n$((lineno-1)) "${inputfile}"
+ tail +$((lineno+1)) "${inputfile}"; } > "$tmpf"
mv "$tmpf" "${inputfile}"
diff --git a/tsvdel.1.in b/tsvdel.1.in
@@ -6,7 +6,7 @@
.Nd Delete a line from a TSV file
.Sh SYNOPSIS
.Nm
-.Op Ar tsvfile.tsv
+.Ar tsvfile.tsv
.Op Ar rowno | Ar colno Ar text
.Sh DESCRIPTION
.
diff --git a/tsvedit b/tsvedit
@@ -1,14 +1,16 @@
#!/bin/sh
# vim: set ft=bash:
# tsvedit - Edit a line from a TSV file
-# See the file LICENSE for copyright and license details.
+# This program is licensed under the terms of GNU GPL v3 or (at your option)
+# any later version. Copyright (C) 2023 Страхиња Радић.
+# See the file LICENSE for exact copyright and license details.
# Needs vipe from moreutils and $EDITOR https://joeyh.name/code/moreutils/
usage()
{
cat <<EOT
-Usage: ${0##*/} [tsvfile.tsv] [[rowno] | [colno] [text]]
+Usage: ${0##*/} tsvfile.tsv [rowno | colno text]
EOT
}
@@ -53,14 +55,14 @@ if [ "${lineno}" -lt 1 ]; then
exit 1
fi
-head -n$((lineno-1)) "${inputfile}" > "$tmpf"
-{ sed 1q "${inputfile}" | sed -e 's/^/# /' -e 's/\t/\t# /g'
- head -n"$lineno" "${inputfile}" | tail -n1; } | transpose - \
- | sed 's/\t/\n/g' \
-| vipe \
- | sed '/^#/d;s/\([^\t]*\)$/\1\t/g' | tr -d '\n' \
- | sed 's/\t$/\n/g' >> "$tmpf"
-tail +$((lineno+1)) "${inputfile}" >> "$tmpf"
+{ head -n$((lineno-1)) "${inputfile}"
+ { sed 1q "${inputfile}" | sed -e 's/^/# /' -e 's/\t/\t# /g'
+ head -n"$lineno" "${inputfile}" | tail -n1; } |
+ transpose - | sed 's/\t/\n/g' |
+ vipe --suffix tsv |
+ sed '/^#/d;s/\([^\t]*\)$/\1\t/g' | tr -d '\n' |
+ sed 's/\t$/\n/g'
+ tail +$((lineno+1)) "${inputfile}"; } > "$tmpf"
if diff "$tmpf" "${inputfile}" >/dev/null 2>&1; then
rm "$tmpf"
diff --git a/tsvedit.1.in b/tsvedit.1.in
@@ -6,7 +6,7 @@
.Nd Edit a line from a TSV file
.Sh SYNOPSIS
.Nm
-.Op Ar tsvfile.tsv
+.Ar tsvfile.tsv
.Op Ar rowno | Ar colno Ar text
.Sh DESCRIPTION
.
diff --git a/tsvins b/tsvins
@@ -1,14 +1,16 @@
#!/bin/sh
# vim: set ft=bash:
# tsvins - Insert a line to a TSV file
-# See the file LICENSE for copyright and license details.
+# This program is licensed under the terms of GNU GPL v3 or (at your option)
+# any later version. Copyright (C) 2023 Страхиња Радић.
+# See the file LICENSE for exact copyright and license details.
# Needs vipe from moreutils and $EDITOR https://joeyh.name/code/moreutils/
usage()
{
cat <<EOT
-Usage: ${0##*/} [tsvfile.tsv] [[lineno]]
+Usage: ${0##*/} tsvfile.tsv [lineno]
EOT
}
@@ -32,7 +34,7 @@ esac
[ ! -f "${inputfile}" ] && touch -- "${inputfile}"
tmpf=${inputfile}~
-lines=$(wc -l -- "${inputfile}" | awk '{print $1}')
+numlines=$(wc -l -- "${inputfile}" | awk '{print $1}')
command -v vipe >/dev/null 2>&1 || { error "vipe not found"; exit 1; }
@@ -43,18 +45,17 @@ trap "rm -f -- \"$tmpf\"" HUP PIPE INT QUIT TERM EXIT
if [ $# -eq 2 ]; then
head -n"${lineno}" -- "${inputfile}" > "$tmpf"
fi
-if [ "${lines}" -gt 0 ]; then
+if [ "${numlines}" -gt 0 ]; then
{ sed 1q -- "${inputfile}" | sed -e 's/^/# /' -e 's/\t/\t# /g'
- sed 1q -- "${inputfile}" | sed -e 's/[^\t]*\(\t\?\)/\1/g'; } \
- | transpose - \
- | sed -e 's/\t/\n/g' \
- | vipe --suffix tsv \
- | sed '/^#/d;s/\([^\t]*\)$/\1\t/g' | tr -d '\n' \
- | sed 's/\t$/\n/g' >> "$tmpf"
+ sed 1q -- "${inputfile}" | sed -e 's/[^\t]*\(\t\?\)/\1/g'; } |
+ transpose - | sed -e 's/\t/\n/g' |
+ vipe --suffix tsv |
+ sed '/^#/d;s/\([^\t]*\)$/\1\t/g' | tr -d '\n' |
+ sed 's/\t$/\n/g' >> "$tmpf"
else
- vipe --suffix tsv </dev/null | \
- sed 's/\([^\t]*\)$/\1\t/g' | tr -d '\n' \
- | sed 's/\t$/\n/g' >> "$tmpf"
+ vipe --suffix tsv </dev/null |
+ sed 's/\([^\t]*\)$/\1\t/g' | tr -d '\n' |
+ sed 's/\t$/\n/g' >> "$tmpf"
fi
if [ $# -eq 2 ]; then
tail +$((lineno + 1)) -- "${inputfile}" >> "$tmpf"
diff --git a/tsvins.1.in b/tsvins.1.in
@@ -6,7 +6,7 @@
.Nd Insert a line to a TSV file
.Sh SYNOPSIS
.Nm
-.Op Ar tsvfile.tsv
+.Ar tsvfile.tsv
.Op Ar lineno
.Sh DESCRIPTION
.
diff --git a/tsvmove b/tsvmove
@@ -0,0 +1,81 @@
+#!/bin/sh
+# vim: set ft=bash:
+# tsvmove - Move a line in a TSV file
+# This program is licensed under the terms of GNU GPL v3 or (at your option)
+# any later version. Copyright (C) 2023 Страхиња Радић.
+# See the file LICENSE for exact copyright and license details.
+
+usage()
+{
+ cat <<EOT
+Usage: ${0##*/} tsvfile.tsv [rowno | colno text] rowno
+EOT
+}
+
+error()
+{
+ printf "%s: %s\n" "${0##*/}" "$@" >&2
+}
+
+case $# in
+ 3) inputfile=$1
+ linenofrom=$2
+ linenoto=$3
+ ;;
+ 4) inputfile=$1
+ colnofrom=$2
+ searchtextfrom=$3
+ linenoto=$4
+ ;;
+ *) usage
+ exit 1
+ ;;
+esac
+
+tmpf=${inputfile}~
+
+[ -w "${inputfile}" ] || { error "\`${inputfile}' not writeable"; exit 1; }
+
+cp "${inputfile}" "$tmpf"
+# shellcheck disable=SC2064
+trap "rm -f \"$tmpf\"" HUP PIPE INT QUIT TERM EXIT
+
+if [ -z "${linenofrom}" ]; then
+ linenofrom=$(cut -d"$(printf '\t')" -f"${colnofrom}" "${inputfile}" |
+ grep -ni "${searchtextfrom}" |
+ sed 's/^\([0-9]\+\).*/\1/;1q')
+ # shellcheck disable=SC2140
+ [ -z "$linenofrom" ] \
+ && { error "not found \`${searchtextfrom}' in column "\
+"${colnofrom}"
+ exit 1; }
+fi
+
+numlines=$(wc -l -- "${inputfile}" | awk '{print $1}')
+if [ "${linenofrom}" -gt "${numlines}" ]; then
+ error "line number out of range: ${linenofrom}"
+ exit 1
+elif [ "${linenoto}" -gt "${numlines}" ]; then
+ error "line number out of range: ${linenoto}"
+ exit 1
+fi
+
+if [ "${linenofrom}" -lt "${linenoto}" ]; then
+ delta=$((linenoto - linenofrom))
+ { head -n$((linenofrom-1)) "${inputfile}"
+ tail +$((linenofrom+1)) "${inputfile}" |
+ head -n"${delta}"
+ head -n$((linenofrom)) "${inputfile}" |
+ tail -n1
+ tail +$((linenoto+1)) "${inputfile}"; } > "$tmpf"
+ mv "$tmpf" "${inputfile}"
+elif [ "${linenofrom}" -gt "${linenoto}" ]; then
+ delta=$((linenofrom - linenoto))
+ { head -n$((linenoto-1)) "${inputfile}"
+ head -n$((linenofrom)) "${inputfile}" |
+ tail -n1
+ tail +$((linenoto)) "${inputfile}" |
+ head -n"${delta}"
+ tail +$((linenofrom+1)) "${inputfile}"; } > "$tmpf"
+ mv "$tmpf" "${inputfile}"
+fi
diff --git a/tsvmove.1.in b/tsvmove.1.in
@@ -0,0 +1,28 @@
+.Dd %DATE%
+.Dt TSVMOVE 1
+.Os
+.Sh NAME
+.Nm tsvmove
+.Nd Move a line in a TSV file
+.Sh SYNOPSIS
+.Nm
+.Ar tsvfile.tsv
+.Op Ar rowno | Ar colno Ar text
+.Ar rowno
+.Sh DESCRIPTION
+.
+.Bl -tag -width ".Nm Ar rowno" -offset indent
+.
+.It Nm Ar tsvfile.tsv Ar rowno Ar rowno
+select the line (row) to be moved by its row number (1\-based)
+.
+.It Nm Ar tsvfile.tsv Ar colno Ar text Ar rowno
+select the line (row) to be moved by searching the column with index
+.Ar colno
+(1\-based) for
+.Ar text .
+.
+.El
+.Sh AUTHORS
+.An "Strahinya Radich" Aq contact@strahinja.org ,
+2023