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
# 8  
Old 10-10-2013
Quote:
Originally Posted by Natalie
I am not fan of awk, also dont understand it. So i will try to replace it with statements... Anyway, thank u so muchSmilie
In the future, to not waste anyone's time, please mention in your problem statement that you are not interested in AWK solutions (or that you require a sh-only solution). This same issue occurred in your other thread.

Quote:
Originally Posted by Akshay Hegde
Sorry I was not knowing Smilie
There is no reason for you to apologize.

Regards,
Alister
This User Gave Thanks to alister For This Post:
# 9  
Old 10-10-2013
Yeh Alister I really disappointed, I have one suggestion don't know its good or not...whether its possible to keep one option while posting thread that interested solution in awk, shell, perl,php,c,etc
# 10  
Old 10-10-2013
Quote:
Originally Posted by Akshay Hegde
I have one suggestion don't know its good or not...whether its possible to keep one option while posting thread that interested solution in awk, shell, perl,php,c,etc
If you would like to contribute code that the OP is not interested in using, feel free. I don't see why it would be a problem. The OP can simply ignore it, and your contribution may be of interest to someone else (if not in the present, then perhaps in the future).

My previous post's intent is solely to request that the OP make any restrictions known at the time that help is requested.

Regards,
Alister
# 11  
Old 10-10-2013
Hello I am still working on my code..... The issue I faced is that i have to have 00 01 02 03 04 05 06 07 08 09 010 011 ...099 100. But my numbers are 1 2 3 4 5 ...How I can change it to different format? Moreover, my number is in the file....so basically it reads number in file compare and increment the number and rewrite file with new number Smilie But my piece of code gives me nothing lol Smilie

for example: file kuku.txt
cat kuku.txt
->2013081001
after running my code
cat kuku.txt
->2013081002


Code:
DIR="/Users/Natalie/1/kuku.txt"
n="Users/Natalie/1/$kuku.txt"
#echo "${n:0:4} ${n:4:2} ${n:6:2} ${n:8:2}"

fourthoct=${n:0:4}
thirdoct=${n:4:2}
secondoct=${n:6:2}
firstoct=${n:8:2}

if [ "$(ls -A $DIR)" ] then
sort /Users/Natalie/1/kuku.txt | tail -1 |

     while read firstoct secondoct thirdoct fourthoct
               do
            if [ $((++firstoct)) -eq 101 ]
                   then

                  firstoct=0
                   if [ $((++secondoct)) -eq 32 ]
                        then
 secondoct=0
                         if [ $((++thirdoct)) -eq 13 ]
                               then
                                thirdoct=0
                                   ((++fourthoct))
fi
fi
fi

echo $fourthoct$thirdoct$secondoct$firstoct >kuku.txt
done
else
echo "Hi its new file" >kuku.txt
fi


Last edited by Natalie; 10-10-2013 at 04:07 PM..
# 12  
Old 10-10-2013
Here is a simple starter solution...
Code:
#!/bin/bash --posix
# num.sh
data="2013102100"
data1="${data:0:4}"
data2="${data:4:2}"
data3="${data:6:2}"
data4="${data:8:2}"
echo "$data1 $data2 $data3 $data4"
for n in {1..99}
do
	data4="0$n"
	echo "$data1 $data2 $data3 $data4"
done
data4="100"
echo "$data1 $data2 $data3 $data4"

Results...
Code:
2013 10 21 00
2013 10 21 01
2013 10 21 02
2013 10 21 03
2013 10 21 04
2013 10 21 05
2013 10 21 06
2013 10 21 07
2013 10 21 08
2013 10 21 09
2013 10 21 010
2013 10 21 011
2013 10 21 012
2013 10 21 013
2013 10 21 014
2013 10 21 015
2013 10 21 016
2013 10 21 017
2013 10 21 018
2013 10 21 019
2013 10 21 020
2013 10 21 021
2013 10 21 022
2013 10 21 023
2013 10 21 024
2013 10 21 025
2013 10 21 026
2013 10 21 027
2013 10 21 028
2013 10 21 029
2013 10 21 030
2013 10 21 031
2013 10 21 032
2013 10 21 033
2013 10 21 034
2013 10 21 035
2013 10 21 036
2013 10 21 037
2013 10 21 038
2013 10 21 039
2013 10 21 040
2013 10 21 041
2013 10 21 042
2013 10 21 043
2013 10 21 044
2013 10 21 045
2013 10 21 046
2013 10 21 047
2013 10 21 048
2013 10 21 049
2013 10 21 050
2013 10 21 051
2013 10 21 052
2013 10 21 053
2013 10 21 054
2013 10 21 055
2013 10 21 056
2013 10 21 057
2013 10 21 058
2013 10 21 059
2013 10 21 060
2013 10 21 061
2013 10 21 062
2013 10 21 063
2013 10 21 064
2013 10 21 065
2013 10 21 066
2013 10 21 067
2013 10 21 068
2013 10 21 069
2013 10 21 070
2013 10 21 071
2013 10 21 072
2013 10 21 073
2013 10 21 074
2013 10 21 075
2013 10 21 076
2013 10 21 077
2013 10 21 078
2013 10 21 079
2013 10 21 080
2013 10 21 081
2013 10 21 082
2013 10 21 083
2013 10 21 084
2013 10 21 085
2013 10 21 086
2013 10 21 087
2013 10 21 088
2013 10 21 089
2013 10 21 090
2013 10 21 091
2013 10 21 092
2013 10 21 093
2013 10 21 094
2013 10 21 095
2013 10 21 096
2013 10 21 097
2013 10 21 098
2013 10 21 099
2013 10 21 100

This User Gave Thanks to wisecracker For This Post:
# 13  
Old 10-10-2013
Thanks. But I kind of do not know where to add this logic to my code.....Gives me error everywhere....
# 14  
Old 10-10-2013
I have no doubt some better solution may exist but until someone shoot one ...
For sure this code can be enhanced a lot (for example adding checking conditions to ensure that the given argument is made of 10 digits) but it may give some clue (at least i hope so).
You can also put it into a function that can then be called later on.
Converting date to second , adding 1 day in second , and converting it back to date make it able to handle leap year and avoid tedious date calculations :

Code:
$ cat increm
#!/usr/bin/sh

d=${1%??}
i=${1#$d}

if [ $i -ge 99 ]
then i=0
_s=$( date -u -d "$(echo $d | sed 's/../-&-/3')" +%s )
n=$(( $_s + 86400 ))
_d=$( date -u -d"1970-01-01 $n sec GMT" +%Y%m%d )
d=$_d
else 
i=$(( i + 1 ))
fi
printf "%s%02d\n" $d $i

$ sh increm 1988022899
1988022900
$ sh increm 1988022999
1988030100
$ sh increm 2013103099
2013103100
$ sh increm 2013103100
2013103101
$ sh increm 2013103199
2013110100
$


Last edited by ctsgnb; 10-11-2013 at 04:46 PM.. Reason: Little Code improvement : replacing -eq with -ge
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