英文:
PowerShell Extract Number Between Square Brackets And Insert Into Following Line
问题
我正在尝试插入从上一行提取的项目编号。我有一个包含用方括号编号的文本组的文件。
例如
一些文本
第1行:[1]
第2行:id = 项目
第1行:[2]
第2行:id = 项目
应更改为:
一些文本
第1行:[1]
第2行:id = 项目 1
第1行:[2]
第2行:id = 项目 2
get-content $filepath | $itemnumber = select-string '(?<=\[)[^]]+(?=\])').matches.value | $iteminsertnumber = select-string -pattern "Item" | $iteminsertnumber.replace("Item","Item $itemnumber") | out-file "D:\Users\j\Desktop\New folder\Environment-p mod.ini"
$filepath = "D:\Users\j\Desktop\New folder\Environment-p.ini"
get-content $filepath | select-string -pattern "(?=\[).*?(?=\])" -context 0, 1 | foreach {$_.matches.value} | -replace $_.context.postcontext('Item',"Item $_") | set-content "D:\Users\j\Desktop\New folder\Environment-p mod.ini"```
<details>
<summary>英文:</summary>
I am trying to insert the item number extracted from preceding line. I have a file with groups of text that are numbered in square brackets.
For example
some text
line 1: [1]
line 2: id = Item
line 1: [2]
line 2: id = Item
Should be changed to:
some text
line 1: [1]
line 2: id = Item 1
line1: [2]
line 2: id = Item 2
$filepath = "D:\Users\j\Desktop\New folder\Environment-p.ini"
get-content $filepath | $itemnumber = select-string '(?<=[)[^]]+(?=])').matches.value | $iteminsertnumber = select-string -pattern "Item" | $iteminsertnumber.replace("Item","Item $itemnumber") | out-file "D:\Users\j\Desktop\New folder\Environment-p mod.ini"
$filepath = "D:\Users\j\Desktop\New folder\Environment-p.ini"
get-content $filepath | select-string -pattern "(?=[).*?(?=])" -context 0, 1 | foreach {$.matches.value} | -replace $.context.postcontext('Item',"Item $_") | set-content "D:\Users\j\Desktop\New folder\Environment-p mod.ini"
</details>
# 答案1
**得分**: 2
以下是代码的中文翻译部分:
```shell
# 使用带有 `-Regex` 标志的 `switch` 可以实现这一目标,以下是一个示例:
$content = @'
some text
line 1: [1]
line 2: id = Item
line 1: [2]
line 2: id = Item
line 1: [123]
line 2: id = Item
'@ -split '\r?\n';
switch -Regex ($content) {
'(?<=\[)[^\]]+' {
# 捕获方括号之间的内容
$value = $Matches[0]
# 输出该行
$_
# 继续下一行
continue
}
# 如果之前有捕获
{ $value } {
# 用一个空格和捕获的值替换行的末尾,包括可能的空白字符
$_ -replace '\s*$', " $value"
# 重置捕获的值
$value = $null
# 继续下一行
continue
}
# 如果不符合以上条件,则输出该行
默认 { $_ }
}
如果你要读取文件,可以使用 -File
参数,逻辑仍然相同:
# 这个外部脚本块允许我们将 `switch` 的输出导向到 `Set-Content`
& {
switch -Regex -File $filepath {
# 在此处逻辑相同
}
} | Set-Content path\to\resultfile.ext
英文:
<!-- language-all: sh -->
A switch
with the -Regex
flag could work for this, as an example:
$content = @'
some text
line 1: [1]
line 2: id = Item
line 1: [2]
line 2: id = Item
line 1: [123]
line 2: id = Item
'@ -split '\r?\n'
switch -Regex ($content) {
'(?<=\[)[^\]]+' {
# capture whats between brackets
$value = $Matches[0]
# output the line
$_
# go to next line
continue
}
# if there was a capture previously
{ $value } {
# replace the end of the line including
# any possible whitespaces before it
# with a space and the captured value
$_ -replace '\s*$', " $value"
# reset the value
$value = $null
# go to next line
continue
}
# output this line if none of the above
Default { $_ }
}
If you're reading a file you would use the -File
parameter, logic is still the same:
# this outer scriptblock allows us to pipe the output
# from the switch to Set-Content
& {
switch -Regex -File $filepath {
# same logic here
}
} | Set-Content path\to\resultfile.ext
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论