Incrementing parts of ten digits number by parts


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Incrementing parts of ten digits number by parts
# 1  
Old 10-10-2013
Incrementing parts of ten digits number by parts

I have number in file which contains date and serial number:
2013101000.

The last two digits are serial number (00). So maximum of serial number is 100.

After reaching 100 it becomes 00 with incrementing 10 which is day with max 31.

after reaching 31 it becomes 00 and increments 10 which is month with maximum 12.

so basically when I run code it rewrites 10 digits with above conditions... My problem is that I dont know how to separate these 4 numbers. If I have "." or smth like that among them then it would be easier but here I have no idea.....

So example:
cat file
2013113098

sh my_code
cat file
2013113099

sh my_code
cat file
2013113100

sh my_code
cat file
2013113101

then I sh my_code 100 times
cat file
2013120100

Moderator's Comments:
Mod Comment Next time, please use code tags for your code and data!

Last edited by vbe; 10-10-2013 at 12:06 PM..
# 2  
Old 10-10-2013
Code:
n=2013113098
echo "${n:0:4} ${n:4:2} ${n:6:2} ${n:8:2}"

# 3  
Old 10-10-2013
Quote:
Originally Posted by jethrow
Code:
n=2013113098
echo "${n:0:4} ${n:4:2} ${n:6:2} ${n:8:2}"

so basically u divided my number into 4 parts right?! So I can increment independently, right? Smilie
For incrementing I just add 1 to ${n:8:2} for example, right?
# 4  
Old 10-10-2013
Longhand on OSX 10.7.5 default bash terminal:-
Code:
Last login: Thu Oct 10 07:41:49 on ttys000
AMIGA:barrywalker~> data=2013101173
AMIGA:barrywalker~> data1=${data:0:4}
AMIGA:barrywalker~> echo "$data1"
2013
AMIGA:barrywalker~> data2=${data:4:2}
AMIGA:barrywalker~> echo "$data2"
10
AMIGA:barrywalker~> data3=${data:6:2}
AMIGA:barrywalker~> echo "$data3"
11
AMIGA:barrywalker~> data4=${data:8:2}
AMIGA:barrywalker~> echo "$data4"
73
AMIGA:barrywalker~> data4=$[ ( $data4 + 1 ) ]
AMIGA:barrywalker~> echo "$data4"
74
AMIGA:barrywalker~> echo "$data1 $data2 $data3 $data4"
2013 10 11 74
AMIGA:barrywalker~> _

Hope this helps...
This User Gave Thanks to wisecracker For This Post:
# 5  
Old 10-10-2013
Code:
#!/bin/bash --posix
# Apple Macbook Pro 13", circa 10-08-2012, OSX 10.7.5, default bash terminal.
# 
# Now obtain any date and add 00 to 099 at the end.
# It is simple to add the last 100 after every day.
# You may have to change pieces to suit you environment and OS etc...
for newdate in {0..365}
do
	# Get epoch time and use IT to do all of the hard work...
	secs=$(date +"%s")
	secs=$[ ( ( ( $secs / 86400 ) * 86400 ) + ( $newdate * 86400 ) ) ]
	for n in {0..99}
	do
		# Date WITH spaces...
		# currentdate=$(date -r $secs +"%Y %m %d")
		# Date WITHOUT spaces...
		currentdate=$(date -r $secs +"%Y%m%d")
		data="0$n"
		# Result WITH spaces...
		# currentdate="$currentdate $data"
		# Result WITHOUT spaces...
		currentdate="$currentdate$data"
		echo "$currentdate"
	done
done
# Now all you have to do is add your I/O and any comparing parts...

This will print a whole years worth of dates with extensions of 00 to 099 to STDOUT.
Code:
AMIGA:barrywalker~> ./Up_Date.sh
2013101100
2013101101
2013101102
2013101103
2013101104
2013101105
2013101106
2013101107
2013101108
2013101109
20131011010
20131011011
20131011012
20131011013
20131011014
20131011015
20131011016
20131011017
20131011018
20131011019
20131011020
20131011021
20131011022
.
.
.
.
.
.
20141011077
20141011078
20141011079
20141011080
20141011081
20141011082
20141011083
20141011084
20141011085
20141011086
20141011087
20141011088
20141011089
20141011090
20141011091
20141011092
20141011093
20141011094
20141011095
20141011096
20141011097
20141011098
20141011099
AMIGA:barrywalker~>


Last edited by wisecracker; 10-10-2013 at 09:51 PM.. Reason: Typo in none code line...
# 6  
Old 10-10-2013
I got the idea, thanks. You helped me a lot. But my last numbers goes only until 09 and then it starts from 02 again. Why? it does not reach even 100 for incrementing days or months.... Sorry just I try to do my best to fix code but I am not good at programming I guess....

Code:
if [ -f "kuku.txt" ]
     then

n=$(sort /Users/Natalie/1/kuku.txt | tail -1)
fourthoct=${n:0:4}
thirdoct=${n:4:2}
secondoct=${n:6:2}
firstoct=${n:8:2}

  wardevent=$(expr $firstoct + 0);
  if [ ! $((++wardevent)) -eq 100 ]
                 then
                      data="0$wardevent"
                  else
                    firstoct=0
                    data="0$firstoct"
                   if [ $((++secondoct)) -eq 32 ]
                         then
                        secondoct=0
                         if [ $((++thirdoct)) -eq 13 ]
                               then
                                thirdoct=0
                                   ((++fourthoct))

fi
fi
fi

printf  $fourthoct$thirdoct$secondoct$data > '/Users/Natalie/1/kuku.txt'
      else
echo ` date +%Y%m%d`"00" >'/Users/Natalie/1/kuku.txt'
fi


Last edited by Natalie; 10-10-2013 at 11:07 PM..
# 7  
Old 10-10-2013
Whether you want like this

Code:
$ cat file
2013113098

Code:
$ cat serial.sh

  awk '

BEGIN{
         OFS="\t"
         printf RS OFS "Enter Year till which you want to generate serial number : "
         getline stop < "-"
         printf OFS "Enter Output Filname :  "
         getline out < "-" 
         nonleap = "31,28,31,30,31,30,31,31,30,31,30,31"
         leap     = "31,29,31,30,31,30,31,31,30,31,30,31"
         split(nonleap,NLP,",")
         split(leap,LP,",")
         if(!stop){printf RS "\t" "No User Input...Exiting" RS RS;exit }
     }

function serial(start,m,d,y){
                                   for(s=start;s<=100;s++)    
                                               if(out){
                                                       print y sprintf("%.2d",m) sprintf("%.2d",d) sprintf("%.3d",s) >out
                                                      } 
                                              else
                                                     {
                                                        print y sprintf("%.2d",m) sprintf("%.2d",d) sprintf("%.3d",s)
                                                     }
                            }

    {
        ser=substr($1,8,10)
        day=substr($1,7,2)
        mon=substr($1,5,2)        
        year=substr($1,0,4)

        if(stop~/[A-Za-z]/){print "Year should be Integer...Exiting" RS;exit}
        if(stop%1!=0){print "Year cannot be float...Exiting" RS;exit}
        if(stop<year){print "Year should be Equal or Greater than  Existing Serial Number...Exiting" RS;exit}

        f=1
        for(y=year;y<=stop;y++){
                mon=(f==1)?mon:1
                
        f=0
                g=1        
                for(m=mon;m<=12;m++)
                    {
                        dend=((y%4==0) || (y%100==0) || (y%400==0))?LP[m]:NLP[m]
                        day=(g==1)?day:1
                        ser=(g==1)?ser:0
                        for(day=day;day<=dend;day++){serial(ser,m,day,y)}
                g=0
                    }                
                                 }
            
     }' file

Resulting

Code:
$ sh serial.sh

  Enter Year till which you want to generate serial number : 2013
  Enter Output Filname :  test.txt

20131130098
20131130099
20131130100
20131201000
20131201001
20131201002
20131201003
20131201004
20131201005
20131201006

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to get first four parts of the string?

I have the string: XXXX.YYYY_ZZZ.20180724.01.txt I need to get rid of .txt and get full four parts XXXX.YYYY_ZZZ.20180724.01 I did: CTL=`echo XXXX.YYYY_ZZZ.20180724.01.txt | rev | cut -d"." -f4 | rev` But got only YYYY_ZZZ What should I do to get all four parts of that... (4 Replies)
Discussion started by: digioleg54
4 Replies

2. Shell Programming and Scripting

Getting various parts from the log

I am dealing with some app log, see example below: 22:16:13.601 ClientSession(905)--Connection(5)--SELECT GETDATE() 22:16:13.632 ClientSession(158)--Connection(5)--SELECT 1 22:16:13.632 ClientSession(848)--Connection(6735)--SELECT 1 So far I needed to collect certain column from it, such as... (3 Replies)
Discussion started by: migurus
3 Replies

3. Shell Programming and Scripting

Combine two parts of a file

Hello All, I have a file like this APPLY ( 'INSERT INTO brdcst_media_cntnt ( cntnt_id ,brdcst_media_cntnt_cd ,cntnt_prvdr_cd ,data_src_type_cd ,cntnt_titl_nm ,cntnt_desc ,batch_dt ,batch_id ) VALUES ( :cntnt_id (3 Replies)
Discussion started by: nnani
3 Replies

4. Shell Programming and Scripting

Extract Parts of File

Hello All, I have a file like this Define schema flat_file_schema ( a varchar(20) ,b varchar(30) ,c varchar(40) ); (Insert into table ( a ,b ,c ) values ( 1 ,2 ,3 ); (4 Replies)
Discussion started by: nnani
4 Replies

5. Shell Programming and Scripting

Parts is parts, but all together ...

I understand the individual pieces of the following (with one exception ..), but how does it all work as one? find ${HOME}/reports/ -name surveyresult*.txt -exec ls -1 {} \; | /usr/xpg4/bin/grep -E \ "${HOME}/reports/surveyresult{14,14}.txt" | sort > ${ResultsFileList} Find all files like... (1 Reply)
Discussion started by: jdorn001
1 Replies

6. Shell Programming and Scripting

[ask]break line number into several parts

hlow all, i have file with wc -l file.txt is 3412112 line number so I want to break these files into several parts with assumsi line 1-1000000 will be create part1.txt and 1000001-2000000 will create part2.txt and 2000001-3000000 will create part3.txt and 3000001-3412112 will create... (5 Replies)
Discussion started by: zvtral
5 Replies

7. Shell Programming and Scripting

Extracting parts of a file.

Hello, I have a XML file as below and i would like to extract all the lines between <JOB & </JOB> for every such occurance. The number of lines between them is not fixed. Anyways to do this awk? ============ <JOB APR="1" AUG="1" DEC="1" FEB="1" JAN="1" JUL="1" JUN="1" MAR="1" MAY="1"... (3 Replies)
Discussion started by: srivat79
3 Replies

8. Shell Programming and Scripting

getting parts of a file

Hello, I'm trying to retreive certain bits of info from a file. the file contains a list like this info1:info2:info3:info4 info1:info2:info3:info4 info1:info2:info3:info4 info1:info2:info3:info4 how do i pick out only info2 or only info3 without the others? Thanks (11 Replies)
Discussion started by: bebop1111116
11 Replies

9. UNIX for Dummies Questions & Answers

cksum parts of a file

Every time we build an executable the date and time are put into the file, I need to run checksum on just the working lines.(IE, no header files) Is this even possible, if so how would I go about it? I am using a HP-UX server any help you can give me will be greatly appreciated. Thanks (6 Replies)
Discussion started by: crazykelso
6 Replies
Login or Register to Ask a Question