Showing 4 digits

Hello everybody
I'm a little beginer for shell script as I started last night...

I have this script

cat fichier.txt | while read l ; do

#echo $l

echo $x
x=$(( $x + 1 ))

done

it's return

1
2
3
4
5
6
7
8
9
10
11
12
....

And I would like it to return a constant lenght of nunber like

0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
....

What can I do?
I guess it should be pretty easy but I researched and didn't find anything.

Thank you for your attention :slight_smile:

R�mi

Hello remobemol,

Welcome to forums, hope you enjoy posting questions, sharing knowledge here. Coming to your question, I am not sure about your complete requirement but seems like you want to print from 1st line to till last line of a Input_file? If this is the case then you was close, could you please try following.

while read l
do
    x=$((x+1))
    printf "%04d\n" $x
done < "Input_file"

Please put your file name in place of Input_file above.

Thanks,
R. Singh

thank you so much for your welcome.

I was thinking about to use that, the problem is that originaly I had a script witch allow me to include this number in a comand to download some file.

count=0
while read p; do
  echo wget -O "$2_img${count}.jpg" $p
  count=$((count+1))
  
done <2

then, with your script I manadge to print all the line of the file,
but then it's does't seem to work to create command

I put echo to debug it but I want to make it part of a comand with wget.

I hope I'm clear enough for you to understand.

Change the:

  echo wget -O "$2_img${count}.jpg" $p

to:

  echo wget -O "$2_img$(printf '%04d' "$count").jpg" $p

and get rid of the echo when you are done debugging.

PS If you tell us what operating system and shell you're using, we might also be able to suggest some shell specific shortcuts to do this. Depending on what shell and (and version of that shell) you're using, there might be shortcuts that would run slightly faster, but would be less portable.

1 Like

I would like to down load a list of file witch their URL are in a file that we will name file.txt

my script so far is

while read p; do
  echo wget -O "img${count}.jpg" $p
  count=$((count+1))
  
done < file.txt

It's work very well just that the output is that I have a filename with different numbers of characters.

img1
img2
img3
img4
img5
img6
img7
img8
img9
img10
img11
img12

witch cause sorting problems as the 10 arrive before the 1.

and I would love to get

img0001
img0002
img0003
img0004
img0005
img0006
img0006
img0007
img0008
img0009
img0010
img0011
img0012

to be fair that's not so important and a way to fix the problem is to put $count=1000 before the loop, like that it's start from 1000.

If I ask that it's more about to do a clean script in order to learn.

Thank you very much

---------- Post updated at 06:08 PM ---------- Previous update was at 05:42 PM ----------

thank you so much everybody
That's work exaclty as I expected.