feat(unitproc/bkt-remove_leading_zeroes):Add bash function
[BK-2020-03.git] / unitproc / bkt-remove_leading_zeroes
CommitLineData
bf9fed34
SBS
1#!/bin/bash
2
3read_stdin() {
4 # Desc: Consumes stdin; outputs as stdout lines
5 # Input: stdin (consumes)
6 # Output: stdout (newline delimited)
7 # return 0 stdin read
8 # return 1 stdin not present
9 # Example: printf "foo\nbar\n" | read_stdin
10 # Depends: GNU bash (version 5.1.16), GNU Coreutils 8.32 (cat)
11 # Version: 0.1.1
12 # Attrib: Steven Baltakatei Sandoval (2024-01-29). reboil.com
13 local input_stdin output;
14
15 # Store stdin
16 if [[ -p /dev/stdin ]]; then
17 input_stdin="$(cat -)" || {
18 echo "FATAL:Error reading stdin." 1>&2; return 1; };
19 else
20 return 1;
21 fi;
22
23 # Store as output array elements
24 ## Read in stdin
25 if [[ -n $input_stdin ]]; then
26 while read -r line; do
27 output+=("$line");
28 done < <(printf "%s\n" "$input_stdin") || {
29 echo "FATAL:Error parsing stdin."; return 1; };
30 fi;
31
32 # Print to stdout
33 printf "%s\n" "${output[@]}";
34
35 return 0;
36}; # read stdin to stdout lines
37remove_leading_zeroes() {
38 # Desc: Removes leading zeroes from lines
39 # Input: stdin
40 # Output: stdout
41 # Depends: BK-2020-03 read_stdin()
42 # Version: 0.0.1
43 while read -r line; do
44 printf "%s\n" "$line" | sed -E -e 's/(^0*)([0-9].*)/\2/';
45 done < <(read_stdin);
46};
47
48printf "00000.jpg\n0001.jpg\n2.jpg\n000003.jpg\n0010.jpg\n";
49printf "========================\n";
50printf "00000.jpg\n0001.jpg\n2.jpg\n000003.jpg\n0010.jpg\n" | remove_leading_zeroes;
51