From: Steven Baltakatei Sandoval Date: Mon, 20 Feb 2023 10:31:51 +0000 (+0000) Subject: feat(user/space4):Add script to space out stdin every 4 lines X-Git-Tag: 0.8.1~6 X-Git-Url: https://zdv2.bktei.com/gitweb/BK-2020-03.git/commitdiff_plain/8942e5363f55f2795a1de89162d99546262a0f07?ds=sidebyside;hp=--cc feat(user/space4):Add script to space out stdin every 4 lines --- 8942e5363f55f2795a1de89162d99546262a0f07 diff --git a/user/space4 b/user/space4 new file mode 100755 index 0000000..823f412 --- /dev/null +++ b/user/space4 @@ -0,0 +1,52 @@ +#!/usr/bin/env bash +# Desc: Adds blank line every 4 lines +# Usage: cat file | space4 +# Example: seq 1 10 | space 4 +# Version: 0.0.1 +# Depends: Bash 5.1.16 + +yell() { echo "$0: $*" >&2; } # print script path and all args to stderr +die() { yell "$*"; exit 111; } # same as yell() but non-zero exit status +must() { "$@" || die "cannot $*"; } # runs args as command, reports args if command fails +read_stdin() { + # Desc: Consumes stdin; outputs as stdout lines + # Input: stdin (consumes) + # Output: stdout (newline delimited) + # Example: printf "foo\nbar\n" | read_stdin + # Depends: GNU bash (version 5.1.16) + # Version: 0.0.1 + local input_stdin output; + + # Store stdin + if [[ -p /dev/stdin ]]; then + input_stdin="$(cat -)"; + fi; + + # Store as output array elements + ## Read in stdin + if [[ -n $input_stdin ]]; then + while read -r line; do + output+=("$line"); + done < <(printf "%s\n" "$input_stdin"); + fi; + + # Print to stdout + printf "%s\n" "${output[@]}"; +}; # read stdin to stdout lines +main() { + # Depends: BK-2020-03: yell(), die(), must(), read_stdin() + if [[ $# -gt 0 ]]; then die "FATAL:This script uses no positional arguments.:$#"; fi; + n=0; + while read -r line; do + printf "%s\n" "$line"; + if ! (( (n + 1) % 4 )); then + printf "\n"; + fi; + ((n++)); + done < <(read_stdin); +}; # main program + +main "$@"; + +# Author: Steven Baltakatei Sandoval +# License: GPLv3+