Here is my string where I am trying to replace field 4 on the 1st line with the last field on line 2.
SBRP18TOWNOFALMOND*LM~
NM1IL1KxxxSERxxMMIWRK0012345004~
If I manually search and replace in TextPad with these regular expressions it works fine:
(SBR*P*\d)(*)(*\S*\nNM1*\S*)(*\S*)~
(\1)(\4)(\3)(\4)~
This code never finds a match…
[IO.Directory]::SetCurrentDirectory((Convert-Path (Get-Location -PSProvider FileSystem)))
$filetxt = [IO.File]::ReadAllText("C:\test2\QTR2WHCfull\QTR2WHC\test\81xxx1918WIMedicaidHosp1_837_5.DAT")
$filetxt = ($filetxt -match "(?ms)(SBR\*\w\*\d{2})(\*)(\*\S*\nNM1\*\S*)(\*\S*)~", "(\1)(\4)(\3)(\4)~")
$filetxt #this outputs False which I assume it is NOT finding the expression.
I haven't gotten to this line because it is not finding a match above.
That worked! How do I get that to do a replace for all files in a folder. You can see my attempt above. I attempted my code above but it didn’t error or update.
For some reason the input file has to be unix text (linefeeds only), not windows text (linefeeds and carriage returns). “get-content -raw” should work as well.
$a = 'SBR*P*18**TOWNOFALMOND*****LM~
NM1*IL*1*KxxxSE*Rxx*M***MI*WRK0012345004~'
set-content input $a
emacs input # appears to be unix text
Replacing \n with .? will fix it. Just . will match too much. \n won’t match a windows carriage return. The s in (?ms) lets .* include line breaks. \r\n works too for windows text, or \s\s, or … In fact you don’t need the (?ms) if you use \s\s or \r\n. Or just use (?s) with .*? I’m not sure what the m does.