Find the answer to your Linux question:
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 ...
  1. #1
    Just 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

  2. #2
    Linux Engineer Kieren's Avatar
    Join Date
    Aug 2007
    Location
    England
    Posts
    845
    You can just do an ls on the directory if that helps?

    Code:
    for i in `ls *.cvs`
    do
    print "${i}"
    done
    Linux User #453176

  3. #3
    Just 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.

  4. #4
    Just Joined!
    Join Date
    Jan 2007
    Posts
    9
    i=`printf '%.03d' $i`

    i got it, i think

  5. #5
    Linux User
    Join Date
    Nov 2009
    Location
    France
    Posts
    292
    Code:
    for i in {0001..1500};do echo $i;done
    seems to be what you want.

  6. #6
    Just 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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...