feat(unitproc/bkt-remove-leading_zeroes):Remove read_stdin()
[BK-2020-03.git] / user / zeropad.sh
index e008b7192fe19355ab1fbbae1b34bd40853708af..242b30be2e8399d3175f5bced308494b9c184378 100755 (executable)
@@ -1,35 +1,74 @@
 #!/bin/bash
 # Desc: Zero-pad working dir files with initial digits for sorting
 #!/bin/bash
 # Desc: Zero-pad working dir files with initial digits for sorting
-# Usage: zeropad.sh
-# Version: 0.0.5
+# Usage: zeropad.sh [str fext]
+# Example: zeropad.sh;
+#          zeropad.sh ".jpg";
+# Input: arg1  str  file extension of files to apply changes to
+# Version: 0.2.0
+
+declare fext; # file extension
 
 list_files() {
     # Desc: Lists working directory file basenames
     # Usage: list_files
 
 list_files() {
     # Desc: Lists working directory file basenames
     # Usage: list_files
-    # Input: none
+    # Input: fext  var  file extension
     # Output: stdout  a line-delimited list of file names
     # Output: stdout  a line-delimited list of file names
-    find . -mindepth 1 -maxdepth 1 -type f -exec basename '{}' \; ;
+    if [[ -z "$fext" ]]; then
+        find . -mindepth 1 -maxdepth 1 -type f -exec basename '{}' \; ;
+    else
+        find . -mindepth 1 -maxdepth 1 -type f -exec basename '{}' \; \
+            | grep -E -- "${fext}$" ;
+    fi;
 }; # list pwd file basenames
 }; # list pwd file basenames
-main () {
+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;
+};
+get_max_digits() {
+    # Desc: Returns max leading digits of pwd filenames
+    # Depends: list_files()
+    #          BK-2020-03:  remove_leading_zeroes()
+    #          GNU sed 4.8
+    #          GNU awk 5.1.0
+    local output;
+    
     # Find the maximum number of leading digits in the filenames of working dir
     # Find the maximum number of leading digits in the filenames of working dir
-    max_digits="$(list_files | sed -e 's/[^0-9].*//' | awk '{ if(length > L) L=length } END { print L }')";
+    output="$(list_files | remove_leading_zeroes \
+        | awk '{ match($0, /^[0-9]+/); if (RLENGTH > max) max=RLENGTH } END { print max }' )";
     # declare -p max_digits; # debug
     # declare -p max_digits; # debug
+    printf "%s" "$output";
+}; # return max digits to stdout
+main () {
+    # Read file extension if provided
+    if [[ -n "$1" ]]; then
+        fext="$1";
+    fi;
+
+    max_digits="$(get_max_digits)";
 
     # Loop over the files and rename them
     while read -r file; do
 
     # Loop over the files and rename them
     while read -r file; do
+        # Skip files with no leading digits
         re='^[0-9]+';
         if [[ ! "$file" =~ $re ]]; then continue; fi;
 
         # Extract the leading digits
         re='^[0-9]+';
         if [[ ! "$file" =~ $re ]]; then continue; fi;
 
         # Extract the leading digits
-        digits="$(echo "$file" | sed 's/\([0-9]*\).*/\1/')";
+        digits="$(echo "$file" | sed -E 's/([0-9]*).*/\1/')"; # all leading digits
+        digits_nlz="$(echo "$digits" | remove_leading_zeroes)"; # no leading zeroes
         # Zero-pad the digits
         # Zero-pad the digits
-        padded_digits="$(printf "%0${max_digits}d" "$digits")";
+        padded_digits="$(printf "%0${max_digits}d" "$digits_nlz")";
         # Construct the new filename
         new_file="${padded_digits}${file#${digits}}";
         # Rename the file
         if [[ "$file" == "$new_file" ]]; then continue; fi;
         # Construct the new filename
         new_file="${padded_digits}${file#${digits}}";
         # Rename the file
         if [[ "$file" == "$new_file" ]]; then continue; fi;
-        mv -n "$file" "$new_file"
-        # declare -p file new_file; # debug
+        mv -n "$file" "$new_file";
+        #declare -p max_digits file digits digits_nlz padded_digits new_file # debug
     done < <(list_files);
 }; # main program
 
     done < <(list_files);
 }; # main program