Home > database >  Is there a way to change multiple lines with one loop in Power Shell
Is there a way to change multiple lines with one loop in Power Shell

Time:12-13

I have a text file like this:

info

:

I went to Paris

xxx

yyy

zzz

info

:

I went to Italy

aaa

bbb

ccc

I want this text file to be like this

Info : I went to Paris

xxx

yyy

zzz

Info : I went to Italy

aaa

bbb

ccc

So it will be like;

1- finding every colons

2- (a way) double pressing to backspace button and moving to upper line and pressing spacebar colon spacebar delete button which will get those paris and italy lines to the upper line.

CodePudding user response:

Use the regex-based -replace operator:

Tip of the hat to Santiago Squarzon for helping to simplify the regex and substitution.

# Outputs to the screen; pipe to Set-Content to save back to a file as needed.
(Get-Content -Raw file.txt) -replace '(?m)(?<=^info)(?:\r?\n){2}:(?:\r?\n){2}', ' : '

For an explanation of the regex and the ability to experiment with it, see this regex101.com page.

  • Related