Showing 4 digits


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Showing 4 digits
# 1  
Old 08-13-2016
Showing 4 digits

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

I have this script

Code:
cat fichier.txt | while read l ; do

#echo $l

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

done

it's return

Code:
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
Code:
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 :-)

Rémi
# 2  
Old 08-13-2016
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.
Code:
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
# 3  
Old 08-13-2016
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.
Code:
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.
# 4  
Old 08-13-2016
Quote:
Originally Posted by remibemol
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.
Code:
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:
Code:
  echo wget -O "$2_img${count}.jpg" $p

to:
Code:
  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.

Last edited by Don Cragun; 08-13-2016 at 01:38 PM.. Reason: Add PS.
This User Gave Thanks to Don Cragun For This Post:
# 5  
Old 08-13-2016
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
Code:
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.

Code:
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

Code:
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

sed / awk script to delete the two digits from first 3 digits

Hi All , I am having an input file as stated below 5728 U_TOP_LOGIC/U_CM0P/core/u_cortexm0plus/u_top/u_sys/u_core/r03_q_reg_20_/Q 011 611 U_TOP_LOGIC/U_CM0P/core/u_cortexm0plus/u_top/u_sys/u_core/r04_q_reg_20_/Q 011 3486... (4 Replies)
Discussion started by: kshitij
4 Replies

2. Programming

6 digits combination

Is there any program that can create 6 digit numbers with: (DIGIT_1)+(DIGIT_2)+(DIGIT_3)+(DIGIT_4)+(DIGIT_5)+(DIGIT_6)=10 Any perl or C also can. Anyone can help me? Thank you (6 Replies)
Discussion started by: Tzeronone
6 Replies

3. UNIX for Advanced & Expert Users

How to replace last 8 digits?

Hi, How I can replace last 8 ZEROS with 22991231? 19523479811841494432A2013052700000000 19523479811730333980A2013052700000000 19523479811417044397A2013052700000000 19523479811205895810C2013010120130131 A9523479811205895810A2013020120130228 19523479811205895810I2013030120130331... (9 Replies)
Discussion started by: jnrohit2k
9 Replies

4. Shell Programming and Scripting

Find filenames with three digits and add zeros to make five digits

Hello all! I've looked all over the internet and this site and have come up a loss with an easy way to make a bash script to do what I want to do. I have a file with a naming convention as follows: 2012-01-18 string of words here 123.jpg 2012-01-18 string of words here 1234.jpg 2012-01-18... (2 Replies)
Discussion started by: Buzzman25
2 Replies

5. Shell Programming and Scripting

help: single digits inflated to 2 digits

Hi Folks Probably an easy one here but how do I get a sequence to get used as mentioned. For example in the following I want to automatically create files that have a 2 digit number at the end of their names: m@pyhead:~$ for x in $(seq 00 10); do touch file_$x; done m@pyhead:~$ ls file*... (2 Replies)
Discussion started by: amadain
2 Replies

6. Shell Programming and Scripting

total last digits

hi group, How can I count total number of 5's which are continuous in the end. i.e. in the below string, the o/p should be 4 I just know to calculate total number of 5's $ echo "95952325555" | awk -F "5" '{print NF-1}' 6 (3 Replies)
Discussion started by: uwork72
3 Replies

7. Shell Programming and Scripting

Formatting digits

I want to check the argument in KSH. If the user type in the prompt 'find 3' it will format 3 to 003 to match the data in the text file. Same as with 10 to 010. Always begins with 0. eg. >find 3 Output: 003 >find 30 Output: 030 (7 Replies)
Discussion started by: harry0013
7 Replies

8. UNIX for Dummies Questions & Answers

Digits display

Hi there, I am new to scripting. Can anyone help me in writing a script which will display all the digits between 1 and 5 inclusive, one digit per line. Should use a loop to do this. Thanks in advance!! (3 Replies)
Discussion started by: Spoorthi16
3 Replies

9. UNIX for Dummies Questions & Answers

Only Digits as input

Hi All, I am new to shell script. I wrote a very small script that takes only digits as input- but there is some problem in that.can you help me in debugging that. #!/bin/ksh echo "Digits as input" read number digit='eval ' if ] then echo "Entered number is a digit" else echo... (2 Replies)
Discussion started by: namishtiwari
2 Replies

10. Shell Programming and Scripting

How to cut last 10 digits off

Hi I'm new to this. I need to cut off the last 10 digits from a line. I've used awk {'print $4'} filename.txt | cut -c 32-42 but this does not guarantee only the last 10 characters. Please help. Thanks. Sara (4 Replies)
Discussion started by: psarava
4 Replies
Login or Register to Ask a Question