Question:
I have some files that contain strings like
1 2 3 4 5 6 |
#define PROG_HWNR "#37595" #define C_HWPROG_NR "24499" #define PROG_HWNR "#39917" #define C_HWPROG_NR "24901" #define C_HWPROG_NR "37598" |
I put them in a file named regex-test (for testing purposes)
What I want to do is increase each number by one. There are other #defines and numbers that must not be increased, I have a list of all variable names that need to be increased.
I already have a PowerShell command like
1 2 3 4 5 6 |
Get-Content regex-test | foreach { [regex]::match($_,'"#?(\d+)" which returns the numbers. They now only need to be replaced with [value] + 1 Can you point me in the right direction? Thanks! |
Answer:
Why not just:
1 2 3 4 |
$tmp = Get-Content regex-test | foreach { $n = [regex]::match($_,'"#?(\d+)" Then save the $tmp . |
Source:
Increasing an number in PowerShell with Regular Expressions by stackoverflow.com licensed under CC BY-SA | With most appropriate answer!
).groups[1].value }
which returns the numbers. They now only need to be replaced with [value] + 1
Can you point me in the right direction? Thanks!
Answer:
Why not just:
1 |
Then save the $tmp
.
Source:
Increasing an number in PowerShell with Regular Expressions by stackoverflow.com licensed under CC BY-SA | With most appropriate answer!
).groups[1].value; if ($n) {$_ -replace "$n", ([int32]$n+1)} else {$_}; }
Then save the $tmp
.
Source:
Increasing an number in PowerShell with Regular Expressions by stackoverflow.com licensed under CC BY-SA | With most appropriate answer!
).groups[1].value }
which returns the numbers. They now only need to be replaced with [value] + 1
Can you point me in the right direction? Thanks!
Answer:
Why not just:
1 |
Then save the $tmp
.