A
A
AlexZeDim2017-02-18 16:37:18
PowerShell
AlexZeDim, 2017-02-18 16:37:18

Powershell -replace -regexp -raw complicated replacement with tabs?

There is a test.lua file (UTF-8) with the content:

function private.PostAuctionsThread(self, auctionInfo)
  self:SetThreadName("SHOPPING_POST_AUCTIONS")
  local auctionRecord = auctionInfo.record
  local postFrame = private.frame.content.result.confirmation.post
  private.frame.content.result.cancelBtn:Disable()
  private.frame.content.result.postBtn:Disable()
  private.frame.content.result.bidBtn:Disable()
  private.frame.content.result.buyoutBtn:Disable()
  postFrame.postBtn:Enable()
  postFrame.closeBtn:Enable()

  local undercut = (TSMAPI:GetCustomPriceValue(TSM.db.global.postUndercut, auctionRecord.itemString) or 1)
  if undercut > auctionRecord.itemBuyout then
    undercut = 1
  end
  if TSMAPI.Player:IsPlayer(auctionRecord.seller, true, true, true) then
    -- don't undercut ourselves
    undercut = 0
  end
  local auctionBuyout
  if auctionRecord.isFake then
    auctionBuyout = TSMAPI:GetCustomPriceValue(TSM.db.global.normalPostPrice, auctionRecord.itemString) or 1000000
  else
    auctionBuyout = auctionRecord.buyout - (undercut * auctionRecord.stackSize)
  end

in which you need to replace the fragment:
<табуляция>	if TSMAPI.Player:IsPlayer(auctionRecord.seller, true, true, true) then
<табуляция><табуляция>		-- don't undercut ourselves
<табуляция><табуляция>		undercut = 0
<табуляция>	end

using a script through Powershell. It would seem nothing easier, right? Well, I do not. And so, there is the script itself:
$text ="фрагмент"
$content = [System.IO.File]::ReadAllText(путьдо\test.lua")
(Get-Content 'путьдо\test.lua')  -replace $test,"example" | Set-Content 'путьдо\test.lua'

which works quite successfully and changes everything, but only within one line . The question is how to make it work within a few lines? And in general, is it possible?
Yes, there are special characters like: `r - carriage return `n - line feed `t - tab `s - space, and you can write something like:
if TSMAPI.Player:IsPlayer(auctionRecord.seller, true, true, true) then`n`t-- don't undercut ourselves...

and even pass the content check, through $text.contains (four`n`t lines`n`t into`n`t one) and the answer will be true
You can simply display $content directly in Powershell, find the desired fragment and through Ctrl+C, Ctrl +V add the necessary fragment to the $text variable
But in practice, all this does not work for some reason.
Perhaps there is some kind of hack with converting strings or by encoding the entire file into string and (hex) with the removal of the necessary elements and converting back with formatting preserved ...
Or you can set the mask in the form of a pattern, where the first line will serve as the beginning, and the last - the first "end" that came across ...
I understand that I beat around the bush, but I stubbornly cannot understand what's the matter. Does anyone have any ideas?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
azarij, 2017-02-18
@AlexZeDim

and if you insert (?s) at the beginning of the description of the fragment to replace?
$a = $b -replace "(?s)blablabla
bla bla bla
bla bla bla"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question