gpt4 book ai didi

string - PowerShell 中的多行字符串到单行字符串的转换

转载 作者:行者123 更新时间:2023-12-01 19:34:29 27 4
gpt4 key购买 nike

我有一个包含多个文本“ block ”的文本文件。这些 block 有多行,并用空行分隔,例如:

This is an example lineThis is an example lineThis is an example lineThis is another example lineThis is another example lineThis is another example line

我需要这些 block 是单行格式,例如

This is an example lineThis is an example lineThis is an example lineThis is another example lineThis is another example lineThis is another example line

我对此进行了彻底的研究,并且只找到了将整个文本文件制作成单行的方法。我需要一种方法(最好在循环中)使字符串 block 数组成为单行。有什么方法可以实现吗?

编辑:我已经编辑了示例内容以使其更加清晰。

最佳答案

# create a temp file that looks like your content
# add the A,B,C,etc to each line so we can see them being joined later
"Axxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Bxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Cxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

Dxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Exxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Fxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

Gxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Hxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Ixxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" | Set-Content -Path "$($env:TEMP)\JoinChunks.txt"

# read the file content as one big chunk of text (rather than an array of lines
$textChunk = Get-Content -Path "$($env:TEMP)\JoinChunks.txt" -Raw

# split the text into an array of lines
# the regex "(\r*\n){2,}" means 'split the whole text into an array where there are two or more linefeeds
$chunksToJoin = $textChunk -split "(\r*\n){2,}"

# remove linefeeds for each section and output the contents
$chunksToJoin -replace '\r*\n', ''

# one line equivalent of above
((Get-Content -Path "$($env:TEMP)\JoinChunks.txt" -Raw) -split "(\r*\n){2,}") -replace '\r*\n', ''

关于string - PowerShell 中的多行字符串到单行字符串的转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41893138/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com