teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

2

修正

2019/06/21 11:56

投稿

Zuishin
Zuishin

スコア28675

answer CHANGED
@@ -138,7 +138,7 @@
138
138
  if ($InputObject -match $FirstPattern) {
139
139
  $replace += ($line + $Count)
140
140
  }
141
- if (($replace -eq $line) -ne $null) {
141
+ if ($replace -contains $line) {
142
142
  $InputObject -replace $SecondPattern, $Replacement
143
143
  $replace = $replace -gt $line
144
144
  } else {

1

追記

2019/06/21 11:56

投稿

Zuishin
Zuishin

スコア28675

answer CHANGED
@@ -83,4 +83,67 @@
83
83
  Get-Content .\old.txt | .\untitled.ps1 bar . x > new.txt
84
84
  ```
85
85
 
86
- 一番目の引数と二番目の引数が正規表現であることに気を付けてください。
86
+ 一番目の引数と二番目の引数が正規表現であることに気を付けてください。
87
+
88
+ # 追記
89
+
90
+ 二度の仕様追加の要件を満たすもののはずです。これ以降、仕様の追加があるのであれば、この質問は解決済みにして自分で実装してください。それでどうしてもわからなければまた改めて新しい質問を立ててください。
91
+
92
+ ```ps1
93
+ [CmdletBinding()]
94
+ Param (
95
+ [Parameter(
96
+ Mandatory = $true,
97
+ Position = 0,
98
+ ValueFromPipelineByPropertyName = $true
99
+ )]
100
+ [regex]$FirstPattern,
101
+
102
+ [Parameter(
103
+ Mandatory = $true,
104
+ Position = 1,
105
+ ValueFromPipelineByPropertyName = $true
106
+ )]
107
+ [regex]$SecondPattern,
108
+
109
+ [Parameter(
110
+ Mandatory = $true,
111
+ Position = 2,
112
+ ValueFromPipelineByPropertyName = $true
113
+ )]
114
+ [string]$Replacement,
115
+
116
+ [Parameter(
117
+ Mandatory = $false,
118
+ Position = 3,
119
+ ValueFromPipelineByPropertyName = $true
120
+ )]
121
+ [ValidateRange(0, [int]::MaxValue)]
122
+ [int]$Count = 5,
123
+
124
+ [Parameter(
125
+ Mandatory = $true,
126
+ Position = 4,
127
+ ValueFromPipelineByPropertyName = $true,
128
+ ValueFromPipeline = $true,
129
+ ValueFromRemainingArguments = $true
130
+ )]
131
+ [string[]]$InputObject
132
+ )
133
+ Begin {
134
+ $replace = @()
135
+ $line = 1
136
+ }
137
+ Process {
138
+ if ($InputObject -match $FirstPattern) {
139
+ $replace += ($line + $Count)
140
+ }
141
+ if (($replace -eq $line) -ne $null) {
142
+ $InputObject -replace $SecondPattern, $Replacement
143
+ $replace = $replace -gt $line
144
+ } else {
145
+ $InputObject
146
+ }
147
+ $line++
148
+ }
149
+ ```