1

I have been searching for answer for many days but I just can't solve it. In my case I need to replace string in .htm file. String is across multiple lines, for example:

</table>
    <p>
<table border="1">

I tried the following but it isn't working and I know because the text appears over several lines.

Get-Content test1.htm )| ForEach{ $_ -replace '</table><p><table border="1">', "" } | Set-Content test2.htm

I have no idea how can I do it.

Matt
  • 45,022
  • 8
  • 78
  • 119
lukesky
  • 29
  • 1
  • 9
  • Do you want to replace always the same string? Or a pattern? Also, what do you want to replace the string with? – Martin Brandl Apr 05 '16 at 12:06
  • In this case I want to replace always the same strin, but for future it will be good to know how to manipulate. I need to remove the string or repalce with another value, so it depends. – lukesky Apr 05 '16 at 12:15
  • well, you should be carefull to replace a string in a html file using the `-replace` cmdlet. You should try to use an XML parser instead. See: http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags – Martin Brandl Apr 05 '16 at 12:35
  • If you have been searching for a while you must have tried something. Do you have example that you have been trying we can help you with. This is a basic request with many related answers already on SO. – Matt Apr 05 '16 at 12:52
  • @Matt ( Get-Content test1.htm )| ForEach{ $_ -replace '

    ', "" } | Set-Content test2.htm But it wont work because the string is 3 lines, like: '
    \n

    \n

    ' So I tried only with powershell.
    – lukesky Apr 05 '16 at 12:58
  • I have included these details in the question for you. If I get a moment I can try and find a duplicate for you. – Matt Apr 05 '16 at 13:05
  • @Matt I ended with this code `$filenames = @("test1.htm") foreach ($file in $filenames) { $replacementStr = '' (Get-Content $file | Out-String) | Foreach-object { $_ -replace '`r`n

    `r`n

    ' , $replacementStr } | Set-Content test2.htm }` but still I'm not able to replace text that appears over several lines. It works if only I want to change text that appear in **one** line
    – lukesky Apr 06 '16 at 07:27

2 Answers2

0

Try this:

(Get-Content 'filename').replace('toreplace', 'replacewith') | Set-Content 'filename'
Martin Brandl
  • 56,134
  • 13
  • 133
  • 172
Will Ryan
  • 661
  • 1
  • 7
  • 17
  • I get the output: Method invocation failed because [System.Object[]] doesn't contain a method named 'replace'. When I do this command there are no result [System.IO.File]::ReadAllText("d:\test.xml").replace('1234','xxxx')|sc d:\test.xml /*this is only an example*/ – lukesky Apr 05 '16 at 12:29
  • `Get-Content -Raw ...` – majkinetor Apr 05 '16 at 12:35
  • A parameter cannot be found that matches parameter name 'Raw'. – lukesky Apr 05 '16 at 12:44
  • @lukesky `(Get-Content 'filename' | Out-String)...` would work since you have PowerShell 2.0. I think you are going to have other issues though. – Matt Apr 05 '16 at 12:50
  • @Matt it is my current code: (Get-Content 'test1.htm' | Out-String).replace('

    ', '') | Set-Content 'test2.htm' but it still not working.
    – lukesky Apr 05 '16 at 13:01
0

OK, somebody managed to solve it, so I post the answer. The cule was to include Poweshell spilt operator: \s+.

So my final code look like:

$filenames = @("test1.htm")foreach ($file in $filenames) {
$replacementStr = ''
(Get-Content $file | Out-String) | 
    Foreach-object { $_ -replace '</table>.\s+<p>\s+<table border="1">' , $replacementStr } | 
 Set-Content test2.htm }
lukesky
  • 29
  • 1
  • 9