Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
how to split a file in not equally large parts?
View unanswered posts
View posts from last 24 hours

Goto page Previous  1, 2  
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
SarahS93
l33t
l33t


Joined: 21 Nov 2013
Posts: 725

PostPosted: Sat Jun 22, 2024 10:56 am    Post subject: Reply with quote

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
View user's profile Send private message
Hu
Administrator
Administrator


Joined: 06 Mar 2007
Posts: 22450

PostPosted: Sat Jun 22, 2024 2:36 pm    Post subject: Reply with quote

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
View user's profile Send private message
SarahS93
l33t
l33t


Joined: 21 Nov 2013
Posts: 725

PostPosted: Sun Jun 23, 2024 4:47 am    Post subject: Reply with quote

.... 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
View user's profile Send private message
szatox
Advocate
Advocate


Joined: 27 Aug 2013
Posts: 3350

PostPosted: Sun Jun 23, 2024 10:07 am    Post subject: Reply with quote

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
View user's profile Send private message
Hu
Administrator
Administrator


Joined: 06 Mar 2007
Posts: 22450

PostPosted: Sun Jun 23, 2024 3:03 pm    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Goto page Previous  1, 2
Page 2 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum