diff --git a/README.md b/README.md index 383b4ac..d0d1787 100644 --- a/README.md +++ b/README.md @@ -22,12 +22,12 @@ list, send a pull request! * [Getting the terminal size (*in a script*).](#getting-the-terminal-size-in-a-script) * [Get the current date using `strftime`.](#get-the-current-date-using-strftime) +* [Get the first N lines in a file.](#get-the-first-n-lines-in-a-file) +* [Get the last N lines in a file.](#get-the-last-n-lines-in-a-file) * [Get the directory name of a file path.](#get-the-directory-name-of-a-file-path) * [Convert a hex color to RGB](#convert-a-hex-color-to-rgb) * [Convert an RGB color to hex.](#convert-an-rgb-color-to-hex) * [Trim quotes from a string.](#trim-quotes-from-a-string) -* [Get the first N lines in a file.](#get-the-first-n-lines-in-a-file) -* [Get the last N lines in a file.](#get-the-last-n-lines-in-a-file) @@ -64,6 +64,35 @@ date() { } ``` + +## Get the first N lines in a file. + +Alternative to the `head` command. + +**NOTE:** Requires `bash` 4+ + +```sh +head() { + # Usage: head "n" "file" + mapfile -tn "$1" line < "$2" + printf '%s\n' "${line[@]}" +} +``` + +## Get the last N lines in a file. + +Alternative to the `tail` command. + +**NOTE:** Requires `bash` 4+ + +```sh +tail() { + # Usage: tail "n" "file" + mapfile -tn 0 line < "$2" + printf '%s\n' "${line[@]: -$1}" +} +``` + ## Get the directory name of a file path. Alternative to the `dirname` command. @@ -106,32 +135,3 @@ trim_quotes() { printf "%s\\n" "${_//\"}" } ``` - - -## Get the first N lines in a file. - -Alternative to the `head` command. - -**NOTE:** Requires `bash` 4+ - -```sh -head() { - # Usage: head "n" "file" - mapfile -tn "$1" line < "$2" - printf '%s\n' "${line[@]}" -} -``` - -## Get the last N lines in a file. - -Alternative to the `tail` command. - -**NOTE:** Requires `bash` 4+ - -```sh -tail() { - # Usage: tail "n" "file" - mapfile -tn 0 line < "$2" - printf '%s\n' "${line[@]: -$1}" -} -```