Skip to content

Commit

Permalink
Merge pull request #52 from Raziel-23/delete_last_line
Browse files Browse the repository at this point in the history
core (delete_last_line): add an optional argument for N deleted lines
  • Loading branch information
mcrapet committed Jun 8, 2016
2 parents be5ee7d + 5951ab2 commit 76ee1ba
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/core.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 76ee1ba

Please sign in to comment.