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
# 15  
Old 10-10-2013
You are changing the goalposts!
Please be more accurate and specific.
I have temporarily removed the loop for obvious reasons.
Fianlly if I am misunderstanding your requirements, then my apologies...
Code:
#!/bin/bash --posix
# num.sh
# Create a null file for fullness IF you want to...
# > /tmp/kuku.txt
data="2013102100"
data1="${data:0:4}"
data2="${data:4:2}"
data3="${data:6:2}"
data4="${data:8:2}"
echo "$data1 $data2 $data3 $data4"
# Notice spaces are now removed as per your EDIT!!!
echo "$data1$data2$data3$data4" > /tmp/kuku.txt
cat < /tmp/kuku.txt
#for n in {1..99}
#do
#	data4="0$n"
#	echo "$data1 $data2 $data3 $data4"
#	echo "$data1$data2$data3$data4" > /tmp/kuku.txt
#	cat < /tmp/kuku.txt
#done
data4="04"
echo "$data1 $data2 $data3 $data4"
echo "$data1$data2$data3$data4" > /tmp/kuku.txt
cat < /tmp/kuku.txt
data4="100"
echo "$data1 $data2 $data3 $data4"
echo "$data1$data2$data3$data4" > /tmp/kuku.txt
cat < /tmp/kuku.txt

Results...
Code:
Last login: Thu Oct 10 19:35:31 on ttys000
AMIGA:barrywalker~> ./num.sh
2013 10 21 00
2013102100
2013 10 21 04
2013102104
2013 10 21 100
20131021100
AMIGA:barrywalker~>

# 16  
Old 10-10-2013
I got confused totally...... My number is in file kuku.txt
cat kuku.txt
->2013101005
According to my code it takes number from the file then compares with numbers and increment +1......But All I got from this code is 1!! Why????
I need to get 2013101006 only

Code:
n= sort /Users/Natalie/1/kuku.txt | tail -1
#n=2013080805
#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}
echo $n


            if [ $((++firstoct)) -eq 101 ]
                   then
                  firstoct=00
                   if [ $((++secondoct)) -eq 32 ]
                        then

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

echo $fourthoct$thirdoct$secondoct$firstoct

# 17  
Old 10-10-2013
Code:
$ cat kuku.txt
2013101005
$ sh increm $(cat kuku.txt)
2013101006
$

(Please see post #14 for the code of increm shell script as well as for the comments ... this may save you tedious date calculations : indeed : what if the month has only 30 days or 28 days ... or if it's a leap year ?)

Add some :
Code:
echo $fourthoct 
echo $thirdoct 
echo $secondoct 
echo $firstoct

In your script to control if the values are correctly setup and if their values behave as you expect

Last edited by ctsgnb; 10-10-2013 at 05:14 PM..
# 18  
Old 10-10-2013
Quote:
Originally Posted by ctsgnb
Code:
$ cat kuku.txt
2013101005
$ sh increm $(cat kuku.txt)
2013101006
$

(Please see post #14 for the code of increm shell script as well as for the comments ...)

Add some :
Code:
echo $fourthoct 
echo $thirdoct 
echo $secondoct 
echo $firstoct

In your script to control if the values are correctly setup and if their values behave as you expect
I did. fourthoct 3oct 2oct 1oct gives nothing as well..... Even i said in my code that 1oct={n:8:2} etc.... I checked my n= sort /Users/yerlanrsaldin/1/kuku.txt | tail -1 it gives me 2013101005...but when I assign it to 1oct 2oct etc then here it does not work.... How i can get the value of the file in path in another way?

Last edited by Natalie; 10-10-2013 at 05:26 PM..
# 19  
Old 10-10-2013
Please provide exactly the command line you've launched in the first run and in the second run.

By the way, the run i've tried with your code when i pass
Code:
2013080805

returns
Code:
201308086

Fyi you could use printf see :
Code:
$ i=6
$ echo $i
6
$ printf '%02d\n' $i
06
$

# 20  
Old 10-10-2013
I found out what is probably wrong in my code. When i assign n=2013101005 instead of path in my code it works. But when I replace by path n=/Users/Natalie/1/kuku.txt (echo $n gives me 2013101005) then echo 1oct
echo 2oct ect gives me nothing. So i think
mine assign 1oct={n:8:2} 2oct {n:6:2} etc does not work here....
# 21  
Old 10-10-2013
You could check the value of $n like this (i guess you forgot what is in red below) :

Code:
n=$(sort /Users/Natalie/1/kuku.txt | tail -1)
echo $n
exit

When you make sure the value is correct you can then remove the exit
This User Gave Thanks to ctsgnb For This Post:
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