Need to represent a number in 99999999 format(8 digits)

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need to represent a number in 99999999 format(8 digits)
# 1  
Old 08-08-2012
Need to represent a number in 99999999 format(8 digits)

Hi all,

i have to create a file having an 8-digit sequence number, that will start by name file_00000001.cvs at first time, the next day the file will be named file_00000002.cvs and so on.

How can i do this in my script please, specially that i will need a counter that increments this number every day.

Please advise

# 2  
Old 08-08-2012
What have you done so far, e.g. what does your script look like?
# 3  
Old 08-08-2012
actually i am stuck at this point and i have not yet implemented this part at all.

this scripts runs daily and creates a file that has the name file_00000001.cvs at first day, file_00000002.cvs at second day and so on

I just wanted to set a counter with format 99999999 that will incremented every day for this purpose.

Please not i work on bash , solaris 10 machine
# 4  
Old 08-08-2012
Try this; it is a bit clumsy as it makes use of the variable a. Me, I'm not aware of how to do bash's parameter expansion immediately on the output if a command.
a=$(basename $(ls file*.csv) .csv); printf file_%0.8d.csv $(( ${a##*_} + 1))

It makes the assumption that a file file_00000001.csv exists in the working dir.

OK, this one is even easier:
printf file_%0.8d.csv $(( $(ls file*.csv|tr -d '[:alpha:][:punct:]') + 1 ))

Last edited by RudiC; 08-08-2012 at 10:13 AM..
# 5  
Old 08-08-2012
this one is very nice:
printf file_%0.8d.csv $(( $(ls file*.csv|tr -d '[:alpha:][:punct:]') + 1 ))


but i have a small problem , the output is concatenated to the shell prompt:
-bash-3.00$ printf file_%0.8d.csv $(( $(ls file_00000001.cvs |tr -d '[:alpha:][:punct:]') + 2 ))

why is that happening :S

Last edited by Franklin52; 08-08-2012 at 10:20 AM.. Reason: Please use code tags for data and code samples
# 6  
Old 08-08-2012
This happens as the code does NOT append a <newline> char which you do not want in your filename. If you want one, make printf's format string like this: "file_%0.8d.csv\n"
# 7  
Old 08-08-2012
# test:
# touch file_00000001.cvs file_00000002.cvs

# last file*.cvs file
last=$( ls -1 file*.cvs 2>/dev/null  | tail -1 )

# remove all a-z . and _ from filename

# if empty = no files => 1st file
[ "$last" = "" ] && last=0 # 1st file

newname=$(printf "file_%0.8d.cvs" $new)
do_something > $newname

Or make cnt file and use it
[ ! -f last.txt ] && echo "0" > last.txt # 1st id
read last < last.txt

newname=$(printf "file_%0.8d.cvs" $new)
# save id
echo "$new" > last.txt
do_something > $newname

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

DB2 convert digits to binary format

Dear Team We use DB2 v10.5 and using DBArtisan tool Can someone please guide how to convert digits to binary numbers using db2 feature. Ex> for number 9 , binary should be 1001 ( 8+1) Any help appreciated. Thanks (2 Replies)
Discussion started by: Perlbaby
2 Replies

2. UNIX for Dummies Questions & Answers

What does the 2nd number after the process id represent ? [ss command]

For instance, when I run ss -lptn | grep sshd , the output is as follows: LISTEN 0 128 *:22 *:* users:(("sshd",1252,3)) 1252 refers to the process id, but what does the 3 refer to ? (2 Replies)
Discussion started by: Hijanoqu
2 Replies

3. Shell Programming and Scripting

Find number of digits in a word

HI, Can you tell me how to find the number of digits in a word. $cat data.txt +123456ad 87645768 Output should be 6 8 (5 Replies)
Discussion started by: ashwin3086
5 Replies

4. Shell Programming and Scripting

awk changes to cut number of digits

HCPM1ONDB00014800011800000589009211201 L201307022013070228AUD 00000000031. 000965105800000000000000000000000 MOBITV KEYA ... (4 Replies)
Discussion started by: mirwasim
4 Replies

5. Shell Programming and Scripting

Represent the data in table format as in mail content

hi all, I want to Represent the data in table format in mail content sample data should be like this...but i m trying to send this table to mail using unix command...hw do i do????even i echoed the table contents ...doesnt work.... help me <table style="background-color:lightblue;">... (8 Replies)
Discussion started by: navsan
8 Replies

6. Shell Programming and Scripting

extracting Number variable and the following digits.

HI all, I have output of something like this: crab: ExitCodes Summary >>>>>>>>> 12 Jobs with Wrapper Exit Code : 50117 List of jobs: 1-12 See https:///twiki/something/ for Exit Code meaning crab: ExitCodes Summary >>>>>>>>> 5 Jobs with Wrapper Exit Code : 8001 List of... (20 Replies)
Discussion started by: emily
20 Replies

7. Shell Programming and Scripting

summing the digits of a binary nuMBER

please help me write a perl program to find the difference of 1 and zeros of a 6 digit binary number. eg If input is 111100 expected output +2 if input is 000011 expected output -2 input is 000111 expected output 0 (2 Replies)
Discussion started by: dll_fpga
2 Replies

8. Shell Programming and Scripting

number of digits after decimal

Hi All, I have a file of decimal numbers, cat file1.txt 1.1382666907 1.2603107334 1.6118799297 24.4995857056 494.7632588468 560.7633734425 ..... I want to see the output as only 7 digits after decimal (5 Replies)
Discussion started by: senayasma
5 Replies

9. Shell Programming and Scripting

Count number of digits in a word

Hi all Can anybody suggest me, how to get the count of digits in a word I tried WORD=abcd1234 echo $WORD | grep -oE ] | wc -l 4 It works in bash command line, but not in scripts :mad: (12 Replies)
Discussion started by: ./
12 Replies

10. UNIX for Advanced & Expert Users

restrain the number of digits of a PID

How is it possible under UNIX to restrain the number of digits of the PID number? For instance, we have a product that generates a PID of 7 digits, and we would like to have only 6 digits maximum instead for the PID. Thank you for your help. (1 Reply)
Discussion started by: mlefebvr
1 Replies
Login or Register to Ask a Question

Featured Tech Videos