question

BB-3218 avatar image
0 Votes"
BB-3218 asked AndreasBaumgarten edited

Change Specific Text in a File

How can I remove a line of text in a specific location with Powershell? Example:

Change Hey, how are you? and change to Hi, how are you?

Leave alone Hey, how are you?

Thanks for your help!



windows-server-powershell
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

BB-3218 avatar image
0 Votes"
BB-3218 answered

I realized that I did not frame this question properly... Here's another go at it.

I am looking to change the text of a specific line, but leave any remaining text within that file alone. Example:

Line 1 of file: Change Hey, how are you? to Hi, how are you?

I would like to change the Hey to Hi.

Line 2 of file : Hey, how are you?

No changes, leave it be.

Thanks to anyone for your help!

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

AndreasBaumgarten avatar image
0 Votes"
AndreasBaumgarten answered

Hi @BB-3218 ,

In the first line you want to replace Hey, how are you?
In the second line you don't want to replace Hey, how are you?

What is the criteria when to replace Hey, how are you? and when not?
Something like even and odd line numbers?


(If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

Regards
Andreas Baumgarten

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

BB-3218 avatar image
0 Votes"
BB-3218 answered

HI Andreas,

Thanks for your response. The issue is only wanting to replace one word in the whole text document. I have seen a whole bunch of stuff online that does a replace, but I don't want to replace all instances. For the first line, I want to replace hey with hi and I do not want to change anything else in the file. I'm OK with using a space in the word hi to fill in the blank space from hey.

Thanks

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

AndreasBaumgarten avatar image
0 Votes"
AndreasBaumgarten answered AndreasBaumgarten edited

Hi @BB-3218 ,

let's try this:

 $txt = "Hey, how are you?
 The quick, brown fox jumps over a lazy dog.
 DJs flock by when MTV ax quiz prog.
 Hey, how are you?
 Junk MTV quiz graced by fox whelps.
 Hey, lets do something
 Bawds jog, flick quartz, vex nymphs.
 Hey, how are you?
 Waltz, bad nymph, for quick jigs vex!
 Fox nymphs grab quick-jived waltz.
 Brick quiz whangs jumpy veldt fox. Bright vixens
 "
    
 [regex]$pattern = "Hey, how are you?"
 $pattern.replace($txt, "Hi, how are you?", 1) 


There are a 3 Hey, how are you? in the text. But only the first will be replaced with Hi, how are you?


(If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

Regards
Andreas Baumgarten




5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.