Increment #'s in text file hourly


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Increment #'s in text file hourly
# 1  
Old 06-16-2010
Increment #'s in text file hourly

I have a text file with a number "001". I am trying to change this number every hour and increment by "1". So every hour it would add +1 to that number. I am able to change the file with sed but unable to have it increment it by "1" without me adding that to the sed command. Any help is appreciated.

sed 's/dist01/dist02/g' myfile

Thank You..
# 2  
Old 06-16-2010
Quote:
Originally Posted by noob33
I have a text file with a number "001". I am trying to change this number every hour and increment by "1". So every hour it would add +1 to that number. I am able to change the file with sed but unable to have it increment it by "1" without me adding that to the sed command. Any help is appreciated.

sed 's/dist01/dist02/g' myfile

Thank You..
Code:
number=`grep -o [0-9]* myfile`
newnumber=$((${number}+1))
sed 's/${number}/${newnumber}/g' myfile

# 3  
Old 06-16-2010
Code:
$ cat data.dat
001
$

Code:
#!/bin/sh

while true; do

oldnr=$(sed 's/^0*//' data.dat)
newnr=$(($oldnr+1))
printf "%03d\n" $newnr > data.dat
sleep 3600

done

# 4  
Old 06-16-2010
Quote:
Originally Posted by pseudocoder
Code:
oldnr=$(sed 's/^0*//' data.dat)


There's no need for sed (or any external command):
Code:
file=data.dat
read num < "$file"
printf "%d\n" "$(( $num + 1 ))" > "$file"

This User Gave Thanks to cfajohnson For This Post:
# 5  
Old 06-16-2010
or eventually
Code:
#!/bin/sh

while true; do

oldnr=$(sed 's/^0*//' data.dat)
newnr=$(($oldnr+1))
newnrwithzeros=$(printf "%03d\n" $newnr)
sed -ie "s/.*/$newnrwithzeros/" data.dat
sleep 3600

done

# 6  
Old 06-16-2010
Hi, Thank You for all the help so far. The problem I am having is getting this number to increment up by 1. I am not sure if its the "" but it does not seem to work correctly.


versionCode="1" and I need the 1 to increment +1 every time the script runs.



Thanks again.

Last edited by noob33; 06-16-2010 at 09:42 PM..
# 7  
Old 06-16-2010
Quote:
Originally Posted by noob33
The problem I am having is getting this number to increment up by 1. I am not sure if its the "" but it does not seem to work correctly.


versionCode="1" and I need the 1 to increment +1
Code:
versionCode=$(( $versionCode + 1 ))

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Combining files(every 15 min) as one file(hourly)

Hello, My system is generating two files every 15 minutes and file names are given automatically as below. (98,99,89,90 are the sequence numbers) File1_09242013131016_000000098 File1_09242013131516_000000099 File2_09242013124212_000000089 File2_09242013124712_000000090 I want to combine... (6 Replies)
Discussion started by: phoenex11
6 Replies

2. Shell Programming and Scripting

Increment value in text file

Hi Guys, I am new to shell programing, I have a csv file which has 50k records and I have got the requirement to increment the value in second column after each 5000 records. for example below A,B,C,D //Header 1,1,London,UK 1,1,Manchester,UK 1,1,Glasgow,UK . . . 1,1,Newyork,USA... (7 Replies)
Discussion started by: rizzu1555
7 Replies

3. Shell Programming and Scripting

Increment Numbers in File

Hello, I have a text file withe some records 20121031|5 20121030|3 20121029|1 20121028|4 20121027|6 I want to search for a patten with '20121030' and then increment the second part of the delimiter i.e. 3 by 1 to make it 4 to look like 20121031|5 20121030|4 20121029|1 20121028|4... (7 Replies)
Discussion started by: pparthiv
7 Replies

4. Shell Programming and Scripting

Create log file periodically ex: hourly

Hello, I have a command which runs continuously and creates output to STDOUT. Now, in unix, if I create logging for this command, it would create a single log file and keep on updating. As there is so much data filled in it, I would want to break the log files periodically. In this instance, say... (4 Replies)
Discussion started by: rajkumarme_1
4 Replies

5. Shell Programming and Scripting

FTP a file on Hourly basis

Hi, I have to upload a file test_201105281100.txt to a ftp location. The files will be created on hourly basis like test_201105281100.txt, test_201105281200.txt & so on. After a file is uploaded successfully, I need to rename the file as test_201105281100.success & if it is not uploaded... (11 Replies)
Discussion started by: SunilB2011
11 Replies

6. Shell Programming and Scripting

Increment a value in a configuration file.

Experts, I would appreciate if someone took the time to express there opinion /approach in creating a new change daily to a configuration file. I create a new log file each day and I wish to have a browser based reader display the new file. To achieve this I would need to create a new... (2 Replies)
Discussion started by: jaysunn
2 Replies

7. Shell Programming and Scripting

File existence and increment

count=0; while read line; do ] && let count=count+1; done < file_name.txt echo echo "$count of 10 files found " echo The scenario is a follows : I have a file which contains a list of filenames present in particular directory . I am checking fo the existence of the file and... (5 Replies)
Discussion started by: ultimatix
5 Replies

8. Shell Programming and Scripting

Check file and increment

My scripts excepts 4 files ABCD_01 ABCD_02 ABCD_03 ABCD_04 I want to check for these files , and increment counter one by one . at the end i would like to echo as 4 of 4 expected instances of file found . I tried something like thsi $counter =1 if counter=counter+1 i need... (5 Replies)
Discussion started by: ultimatix
5 Replies

9. UNIX for Dummies Questions & Answers

count string occurance in a file hourly

Hi, I file that has all the status for one day (24hours). Now what I want to do is to count the occurence of a string in its output hourly like for example count occurance of successful or asynchronous clear destinon for every hour and redirect it to file. Please see sample file below. Please... (2 Replies)
Discussion started by: ayhanne
2 Replies

10. UNIX for Dummies Questions & Answers

How do you automate an hourly file check?

Hi, New to the forum, Great site, I can learn a lot from here!! :cool: I would like to know how to automate a command that checks the Sybase database's are "alive" on an hourly basis, and mails outlook if they are not (1 Reply)
Discussion started by: mals
1 Replies
Login or Register to Ask a Question