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
BR,
Eman
RudiC
August 8, 2012, 7:40am
2
What have you done so far, e.g. what does your script look like?
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
RudiC
August 8, 2012, 7:52am
4
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))
file_00000002.csv
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 ))
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 ))
file_00000003.csv-bash-3.00$
why is that happening :S
RudiC
August 8, 2012, 8:27am
6
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"
kshji
August 8, 2012, 8:31am
7
#!/usr/bin/ksh
# 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
last=${last//[a-z_.]}
# if empty = no files => 1st file
[ "$last" = "" ] && last=0 # 1st file
((new=last+1))
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
((new=last+1))
newname=$(printf "file_%0.8d.cvs" $new)
# save id
echo "$new" > last.txt
#
do_something > $newname
Thank you !! you helped me a lot
RudiC
August 8, 2012, 8:59am
9
I found an error when numbers become larger than 8:
-bash: 00000008: value too great for base (error token is "00000008")
try this modification:
touch $(printf file_%8.8i.csv $(( 10#$(ls file*.csv|tail -1|tr -d '[:alpha:][:punct:]') + 1 )))
it will happily create files ad nauseam; be careful when 99999999 will overflow...
BTW: there is a part in my file for the date i.e it will like: myvf_optout_20120807_00000001.csv
but here removing the underscore using [:punct:] cased the date to be concatenated to the seq number as below
-bash-3.00$ printf "myvf_optout_$yesterday_%0.8d.csv\n" $(( $(ls myvf_optout_"$yesterday"*.csv |tr -d '[:alpha:][:punct:]') + 1 ))
myvf_optout_2012080700000002.csv
-bash-3.00$
Please advise
---------- Post updated at 08:44 AM ---------- Previous update was at 08:25 AM ----------
i did it
-bash-3.00$ printf "myvf_optout_"$yesterday"_%0.8d.csv\n" $(( $(ls myvf_optout_"$yesterday"*.csv | cut -c 21- | tr -d '[:alpha:][:punct:]') + 3 ))
myvf_optout_20120807_00000004.csv
-bash-3.00$
thanks! i will try the correction u did for the 8 digits
RudiC
August 9, 2012, 3:03am
11
Try like this; it is even shorter but relies heavily on your filename's structure not to change:
printf file_%0.8d.csv $(( 10#$(ls file*.csv|tail -1|cut -c15-22) + 1 ))
This works for a four char "file" prefix, you need to adapt the 15-22 to the length of your filename.
---------- Post updated at 03:54 PM ---------- Previous update was at 03:52 PM ----------
Congratulation!
If you use the cut -c N-M
correctly, you can leave out the tr
command entirely but be aware of filename srtucture!
---------- Post updated 09-08-12 at 09:03 AM ---------- Previous update was 08-08-12 at 03:54 PM ----------
If you use awk, things will become far easier, no more heading nor cutting:
touch $(ls file*.csv|awk 'BEGIN {FS="[_\.]"} END {printf "file_%8.8d.csv", $2+1}')
Please adapt to your filenames!
1 Like