user3026339
user3026339

Reputation:

Enclosing a single quote in Awk

I currently have this line of code, that needs to be increased by one every-time in run this script. I would like to use awk in increasing the third string (570).

'set t 570'

I currently have this to change the code, however I am missing the closing quotation mark. I would also desire that this only acts on this specific (above) line, however am unsure about where to place the syntax that awk uses to do that.

awk '/set t /{$3+=1}  1' file.gs >file.tmp && mv file.tmp file.gs

Thank you very much for your input.

Upvotes: 2

Views: 90

Answers (1)

fedorqui
fedorqui

Reputation: 289525

Use sub() to perform a replacement on the string itself:

$ awk '/set t/ {sub($3+0,$3+1,$3)} 1' file
'set t 571'

This looks for the value in $3 and replaces it with itself +1. To avoid replacing all of $3 and making sure the quote persists in the string, we say $3+0 so that it evaluates to just the number, not the quote:

$ echo "'set t 570'" | awk '{print $3}'
570'
$ echo "'set t 570'" | awk '{print $3+0}'
570

Note this would fail if the value in $3 happens more times in the same line, since it will replace all of them.

Upvotes: 2

Related Questions