feat(unitproc/bkt-remove_leading_zeroes):Add bash function
authorSteven Baltakatei Sandoval <baltakatei@gmail.com>
Mon, 15 Jul 2024 22:50:22 +0000 (22:50 +0000)
committerSteven Baltakatei Sandoval <baltakatei@gmail.com>
Mon, 15 Jul 2024 22:50:22 +0000 (22:50 +0000)
unitproc/bkt-remove_leading_zeroes [new file with mode: 0755]

diff --git a/unitproc/bkt-remove_leading_zeroes b/unitproc/bkt-remove_leading_zeroes
new file mode 100755 (executable)
index 0000000..d934e93
--- /dev/null
@@ -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;
+