Operating System - HP-UX
1829596 Members
2057 Online
109992 Solutions
New Discussion

Re: parse and increment by 1

 
SOLVED
Go to solution
Jeff Daigle
Advisor

parse and increment by 1

Hello, I have a file named test.dat which has this in it:

:20:00001

I'm trying to write a script that leaves the :20: at the beginning, but increases the 00001 by 1 each time I run it. I have this so far:

export F1=/jeff/test.dat
N='line < ${F1}'
N2='echo $N | awk -F: 'printf $3}' | sed 's/1/9/g''
N3=`expr ${N2} + 1`
echo "${N3}" > $F1
STAT=$?
exit ${STAT}

It's not quite working. Any help/ suggestions would be appreciated!
5 REPLIES 5
A. Clay Stephenson
Acclaimed Contributor

Re: parse and increment by 1

Hi Jeff,

This is easy but I have one quick question.

Is the :20: constant or are you trying to have a file like this:
:20:000001
:30:000012
:40:000003

Does the file have separate counters or is this just the single entry? If you tell us that the rest is easy.

Clay
If it ain't broke, I can fix that.
John Poff
Honored Contributor
Solution

Re: parse and increment by 1

Jeff,

Here is a cheap way to do it:

#!/bin/sh

F1=/jeff/test.dat
typeset -Z5 NUM=$(awk -F: '{print $3}' $F1)
let NUM=NUM+1
echo ":20:${NUM}" >$F1


Jeff Daigle
Advisor

Re: parse and increment by 1

I am looking for the file to be like this each time:

1st time :20:00001
2nd time :20:00002
3rd time :20:00003

and so on...

Thank you very much!
Jeff Daigle
Advisor

Re: parse and increment by 1

Hi Clay, also, the file doesn't have separate counters. It is just a file that needs to begin with the :20: and then needs a number after the :20: to increase by one each time.

Thanks again!
Jeff
A. Clay Stephenson
Acclaimed Contributor

Re: parse and increment by 1

Hi Jeff,

I attached a 2 minute script to do this. You may need to change the value of F1 to whatever your filename is.

Enjoy, Clay
If it ain't broke, I can fix that.