View previous topic :: View next topic |
Author |
Message |
SarahS93 l33t
Joined: 21 Nov 2013 Posts: 725
|
Posted: Sat Jun 22, 2024 10:56 am Post subject: |
|
|
szatox, thank you - it works!
if i have more than ten files, or generaly
how to create files they called part.01 part.02 part.03 .... part.10 part.11 ....
i will not have the files called part.1 part.2 part.3 .... part.10 part.11 ....
? |
|
Back to top |
|
|
Hu Administrator
Joined: 06 Mar 2007 Posts: 22450
|
Posted: Sat Jun 22, 2024 2:36 pm Post subject: |
|
|
Your filename comes from the value of $counter, so if you want leading zeroes in the filename, include them in $counter. head seems to accept leading zeroes and ignore them. |
|
Back to top |
|
|
SarahS93 l33t
Joined: 21 Nov 2013 Posts: 725
|
Posted: Sun Jun 23, 2024 4:47 am Post subject: |
|
|
.... head -c${i} > "part.0$counter" ....
created files like:
part.01
part.010
part.011
part.02
part.03
part.04
part.05
part.06
part.07
part.08
part.09
that is not what i want |
|
Back to top |
|
|
szatox Advocate
Joined: 27 Aug 2013 Posts: 3350
|
Posted: Sun Jun 23, 2024 10:07 am Post subject: |
|
|
There is some trick with printf, which allows you to specify output format for the parameters, including padding small integers with zeros to ensure constant length.
You can also check length of a string with ${#var}, and then prepend zeros as needed, like here
for i in {8..12}; do if [[ "${#i}" -lt 2 ]]; then echo "0${i}"; else echo "$i"; fi; done
Also, be careful with using 0-padded numbers, bash (and possibly other shells) can interpret them as base 8 integers, which means everything will work as expected in range of 00-07, and then crash when you reach 08 (which is a valid string, but not a valid integer).
Check this out for example:
for i in {05..10}; do echo $((i+1)); done
We don't usually think types when dealing with shell, but there are some cases where it matters. _________________ Make Computing Fun Again |
|
Back to top |
|
|
Hu Administrator
Joined: 06 Mar 2007 Posts: 22450
|
Posted: Sun Jun 23, 2024 3:03 pm Post subject: |
|
|
SarahS93 wrote: | .... head -c${i} > "part.0$counter" ....
created files like:
part.01
part.010
part.011
part.02
part.03
part.04
part.05
part.06
part.07
part.08
part.09
that is not what i want | It's also not what I suggested. I said to put the zeroes in counter, not to add a literal zero adjacent to counter. To expand on szatox's hint, look at printf '%.2u' "$value". |
|
Back to top |
|
|
|