0

I've got the following text file:

dsfgsdgf dsfsdg
Version: 29.15-005
dfs sdfg sdf g
dsfsdfgsdfgsfdg

So it contains a bunch of data and there is a unique line with

Version:

The first time the script is called, 29.15-005 will replaced by 29.15-006. The next time 29.15-006 will be replaced 29.15-007 etc etc

I dont need to check what happens when it gets to -999 as the value will never get that high.

Seen various awk scripts using F and OFS but can't seem to work out how to apply to the above.

Any pointers would be appreciated.

The text file will be different each time its called with other data in it (and different lengths) , but the info from the Version: will remain constant.

Greg
  • 1,715
  • 5
  • 27
  • 36
  • I'm confused about your requirements. It sounds like you have a file with `5` in it and you want a script that when called on that file will output a copy of the file but with the `5` changed to a `6`. Then the next time you call that same script on that same file it will change the `5` to a `7`. wrt `The text file will be different each time its called with other data in it` - that means the file with the `5` will change independently between (during?) invocations of the tool so the `5` will remain a `5` but the rest of the file's contents will change. Please edit your question to clarify. – Ed Morton Jan 07 '16 at 14:10
  • Maybe a little but... are you saying you want the script to change the input file? You say `The text file will be different each time its called` - is that relevant to the problem you're asking for help with? If the answer is "it's not" then please get rid of that statement as it's just obfuscating the question. If the answer is "it is" then please edit your question to state in what way because right now I can't imagine why we'd care if the contents change from one set of data we don't see in your example to some other set of data we don't see in your example. – Ed Morton Jan 07 '16 at 14:28

2 Answers2

1

Try

 awk -F- -v OFS=- '
       /^Version:/{
           incr=sprintf("%03d", ($2 + 1))
           $2=incr
       }1' file > tmpFile && /bin/mv tmpFile file

output

dsfgsdgf dsfsdg
Version: 29.15-006
dfs sdfg sdf g
dsfsdfgsdfgsfdg

The /^Version:/ ensures that the script only processes the line of interest.

The 1 after the closing } char ensures that all lines of the file are printed.

IHTH

shellter
  • 36,525
  • 7
  • 83
  • 90
1

It sounds like what you want might be (using GNU awk for inplace editing):

$ cat file
dsfgsdgf dsfsdg
Version: 29.15-005
dfs sdfg sdf g
dsfsdfgsdfgsfdg

$ awk -i inplace 'BEGIN{FS=OFS="-"} /Version:/{$2=sprintf("%03d",($2+1))} 1' file

$ cat file
dsfgsdgf dsfsdg
Version: 29.15-006
dfs sdfg sdf g
dsfsdfgsdfgsfdg

With other awks do awk 'script' file > tmp && mv tmp file.

Ed Morton
  • 188,023
  • 17
  • 78
  • 185