2

I'm trying to edit a large plain text document containing various html elements such as the following:

  • <p> & </p>
  • <script> & </script>
  • <style> & </style>
  • <div> & </div>
  • And in more extreme cases; <span style="color: #ff0000;"> & </span>

My goal is to remove any <UniqueText> from a text file. I've not worked with powershell much so my knowledge is limited, none less, I gave it a shot.

For replacing all <UniqueText>

get-content "C:\Users\John\Desktop\input.txt" | -replace "\<.*?\>","" | Out-File C:\Users\John\Desktop\output.txt

The above script gives the following error:

-replace : The term '-replace' is not recognized as the name of a cmdlet, function, script file, or operable program.

6
  • 1
    Try ... | % {$_ -replace "\<.*?\>",""} | ... instead. Commented Aug 17, 2018 at 15:20
  • (Get-Content FILE) -replace '\<.*?\>' Commented Aug 17, 2018 at 15:26
  • Are you removing HTML tags from an otherwise plain text file, or are you removing tags from an actual HTML file? For the latter I'd recommend using a proper HTML parser. Commented Aug 17, 2018 at 15:28
  • @AnsgarWiechers It's a plain text file. Sorry, should have stated that in the post. Commented Aug 17, 2018 at 15:29
  • @Paxz That worked like a charm. If you make a post containing that response I will mark it as solved. Commented Aug 17, 2018 at 15:36

2 Answers 2

5

When you use -replace you have to be sure that you parse the string correctly to the call. There are two ways you can solve your problem:

1. Use foreach to go through each line of the file and use -replace on each line (this might be helpfull if you want to do something else with the lines):

get-content "C:\Users\John\Desktop\input.txt" | % {$_ -replace "\<.*?\>",""} | Out-File C:\Users\John\Desktop\output.txt

% is the alias for foreach

$_ is element of the foreach, so each line of the file

2. Use replace on the file without going through each line:

(get-content "C:\Users\John\Desktop\input.txt") -replace "\<.*?\>","" |  Out-File C:\Users\John\Desktop\output.txt
Sign up to request clarification or add additional context in comments.

Comments

1

i use this command

Get-Content C:\Windows\System32\drivers\etc\hosts | % {$_ -replace "some text","" -replace "some text",""} |Out-File C:\Windows\System32\drivers\etc\hosts

but the file become empty.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.