Results 1 to 6 of 6
I'm trying to get a command to loop through a number of files that is in the hundreds. The files are named: 001.cvs 002.cvs etc, up to 650.cvs.
I've tried ...
- 01-13-2010 #1Just Joined!
- Join Date
- Jan 2007
- Posts
- 9
bash script in increase by 1, without losing leading zeros
I'm trying to get a command to loop through a number of files that is in the hundreds. The files are named: 001.cvs 002.cvs etc, up to 650.cvs.
I've tried a number of things, mostly around a basic for loop, but cant get bash to not remove the leading zeros.
for i in {001..650}
do
print "$i".cvs
done
There are no files named 1.cvs, only 001.cvs.
I'm considering a really convoluted thing of hard coding the zeros and some kind of checking thing to see how many place there are with a different loop for 1-9, one for 10-99 and 3rd for 100-650 but that seems really stupid and long.
Any one have any suggestions?
thanks
k
- 01-13-2010 #2
You can just do an ls on the directory if that helps?
Code:for i in `ls *.cvs` do print "${i}" doneLinux User #453176
- 01-13-2010 #3Just Joined!
- Join Date
- Jan 2007
- Posts
- 9
unfortunately, no.
My example is a super simplified version of the script, theres some grepping through files and downloading parts based on that (that I have to scp to get and can't ls) and a whole bunch of other things. print was a poor choice because printf would work. but i am trying to using the variable in other commands.
- 01-13-2010 #4Just Joined!
- Join Date
- Jan 2007
- Posts
- 9
i=`printf '%.03d' $i`
i got it, i think
- 01-13-2010 #5Linux User
- Join Date
- Nov 2009
- Location
- France
- Posts
- 292
seems to be what you want.Code:for i in {0001..1500};do echo $i;done
- 01-13-2010 #6Just Joined!
- Join Date
- Jan 2007
- Posts
- 9
Your suggestion seems the most logical way to do it to me but I could not get it to work that way. printf re-assignment did work even if it was a kind of backwards way to go about it.


Reply With Quote