So let me start of by saying that I'm new to bash so I would appreciate a simple explanation on the answers you give.
I've got the following block of code:
name="Chapter 0000 (sub s2).cbz "
s=$(echo $name | grep -Eo '[0-9]+([.][0-9]+)?' | tr '\n' ' ' | sed 's/^0*//')
echo $s
readarray -d " " -t myarr <<< "$s"
if [[ $(echo "${myarr[0]} < 100 && ${myarr[0]} >= 10" | bc) -ne 0 ]]; then
myarr[0]="0${myarr[0]}"
elif [[ $(echo "${myarr[0]} < 10" | bc) -ne 0 ]]; then
myarr[0]="00${myarr[0]}"
fi
newName="Chapter ${myarr[0]}.cbz"
echo $newName
which (in this case) would end up spitting out:
2
(standard_in) 1: syntax error
(standard_in) 1: syntax error
Chapter .cbz
(I'm fairly certain that the syntax errors are because ${myarr[0]}
is null when doing the comparisons)
This is not the output I want. I want the code to strip leading 0's but leave a single 0 if its all 0.
So the code to really change would be sed 's/^0*//')
but I'm not sure how to change it.
(expected outputs:
in ---> out
1) chapter 8.cbz ---> Chapter 008.cbz
2) chapter 1.3.cbz ---> Chapter 001.3.cbz
3) _23 (sec 2).cbz ---> Chapter 023.cbz
4) chapter 00009.cbz ---> Chapter 009.cbz
5) chap 0000112.5.cbz ---> Chapter 112.5.cbz
6) Chapter 0000 (sub s2).cbz ---> Chapter 000.cbz
so far the code I got works for 1- 3 but not the leading 0 cases (4-6))