From 5951ab24529ecc98b1c6da91d23c42b557308489 Mon Sep 17 00:00:00 2001 From: Raziel-23 Date: Tue, 7 Jun 2016 12:39:20 +0200 Subject: [PATCH] core (delete_last_line): add an optional argument for N deleted lines --- src/core.sh | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/core.sh b/src/core.sh index 00bc1b2a..2c925c3e 100644 --- a/src/core.sh +++ b/src/core.sh @@ -357,10 +357,19 @@ delete_first_line() { sed -ne "$((N+1)),\$p" } -# Delete last line of a text +# Delete last line(s) of a text +# $1: (optional) How many tail lines to delete (default is 1) # stdin: input string (multiline) delete_last_line() { - sed -e '$d' + local -r N=${1:-1} + + if (( N < 1 )); then + log_error "$FUNCNAME: negative index not expected" + return $ERR_FATAL + fi + + # Equivalent to `head -n -1` (if $1=1) + sed -ne :a -e "1,$N!{P;N;D;};N;ba" } # Check if a string ($2) matches a regexp ($1)