- 1 year ago
PowerShell: replace newline and connect two lines
In PowerShell, you can replace newline characters and concatenate two lines using string manipulation techniques. Here's an example:
powershell# Example input lines
$line1 = "Hello,"
$line2 = "World!"
# Replace newline characters with a space
$line1 = $line1 -replace "`r?`n", " "
$line2 = $line2 -replace "`r?`n", " "
# Concatenate the two lines
$concatenatedLine = $line1 + " " + $line2
# Output the result
Write-Host $concatenatedLine
In this example, the -replace
operator is used to replace newline characters (both \r\n
and \n
) with a space using a regular expression. The result is stored in the variables $line1
and $line2
.
Then, the two lines are concatenated using the +
operator, along with an additional space between them, and stored in the variable $concatenatedLine
.
Finally, the concatenated line is outputted using Write-Host
. You can replace this line with the appropriate code for your specific use case, such as writing to a file or displaying it in another context.
Adjust the variables $line1
and $line2
to match your actual input lines, and modify the concatenation logic as needed for your requirements.