提取方括号中的数字并插入到下一行。

huangapple go评论62阅读模式
英文:

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 = @&#39;
some text

line 1: [1]
line 2: id = Item

line 1: [2]
line 2: id = Item

line 1: [123]
line 2: id = Item
&#39;@ -split &#39;\r?\n&#39;

switch -Regex ($content) {
    &#39;(?&lt;=\[)[^\]]+&#39; {
        # 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 &#39;\s*$&#39;, &quot; $value&quot;
        # 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
&amp; {
    switch -Regex -File $filepath {
        # same logic here
    }
} | Set-Content path\to\resultfile.ext

huangapple
  • 本文由 发表于 2023年2月8日 20:47:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/75386025.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定