From: Steven Baltakatei Sandoval Date: Mon, 15 Jul 2024 22:50:22 +0000 (+0000) Subject: feat(unitproc/bkt-remove_leading_zeroes):Add bash function X-Git-Url: https://zdv2.bktei.com/gitweb/BK-2020-03.git/commitdiff_plain/bf9fed34f59ced6a0ffc1196d68803ad27904a0a?ds=inline feat(unitproc/bkt-remove_leading_zeroes):Add bash function --- diff --git a/unitproc/bkt-remove_leading_zeroes b/unitproc/bkt-remove_leading_zeroes new file mode 100755 index 0000000..d934e93 --- /dev/null +++ b/unitproc/bkt-remove_leading_zeroes @@ -0,0 +1,51 @@ +#!/bin/bash + +read_stdin() { + # Desc: Consumes stdin; outputs as stdout lines + # Input: stdin (consumes) + # Output: stdout (newline delimited) + # return 0 stdin read + # return 1 stdin not present + # Example: printf "foo\nbar\n" | read_stdin + # Depends: GNU bash (version 5.1.16), GNU Coreutils 8.32 (cat) + # Version: 0.1.1 + # Attrib: Steven Baltakatei Sandoval (2024-01-29). reboil.com + local input_stdin output; + + # Store stdin + if [[ -p /dev/stdin ]]; then + input_stdin="$(cat -)" || { + echo "FATAL:Error reading stdin." 1>&2; return 1; }; + else + return 1; + 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") || { + echo "FATAL:Error parsing stdin."; return 1; }; + fi; + + # Print to stdout + printf "%s\n" "${output[@]}"; + + return 0; +}; # read stdin to stdout lines +remove_leading_zeroes() { + # Desc: Removes leading zeroes from lines + # Input: stdin + # Output: stdout + # Depends: BK-2020-03 read_stdin() + # Version: 0.0.1 + while read -r line; do + printf "%s\n" "$line" | sed -E -e 's/(^0*)([0-9].*)/\2/'; + done < <(read_stdin); +}; + +printf "00000.jpg\n0001.jpg\n2.jpg\n000003.jpg\n0010.jpg\n"; +printf "========================\n"; +printf "00000.jpg\n0001.jpg\n2.jpg\n000003.jpg\n0010.jpg\n" | remove_leading_zeroes; +