Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to use sed to increase number in file?

Status
Not open for further replies.

Madz

Technical User
Jan 31, 2001
33
SE
I have a file with four numbers that I want to increase every time I run a script.

How to increase the correct number and write it on correct row? (I have the correct number in an variable)

The "number file" could for example look like this:
row1
100 <- should increase by one the next time I run the cript
row2
355 <- &quot;&quot;
row3
400 <- &quot;&quot;



Thanks in advance!
/mats

 
Hi,
you can try this awk script :

BEGIN{i=0}
$0 ~ /[0-9]/ { tab=$0+1}
$0 !~ /[0-9]/ {tab=$0}{i++ }
END{for(j=0;j<i+1;j++) print tab[j] >FILENAME}

make a file which contains this script, then execute

awk -f yourfile.awk filetomodify

if think there is an easy way to do, but this one work, so...

Fcail
 
I am not so familiar with awk. Is 'yourfile.awk' the script file from above?

I got an error that says; Cannot make assignment to tab.It is an array name.

I saved the script from above to: aa.awk
$awk -f aa.awk nrfile
nrfile = the file that contains the numbers I want to increase

How to manage this?


Thanks for your support!

/mats
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top