Purpose: I am currently reading in information and trying to format it as a three column CSV file for future processing (which both the "reading in part" and the "grouping part" are successful). But before the grouping part\building the columns of the CSV, I wish to remove specific words (headers) that throw off the data triplets (this is because as I loop through the data, put 3 items in a row, and then append a newline before repeating until the whole file is read through).
Issue: I am having issues not replacing, but removing a string/element from a .txt file using PowerShell
Code I'm currently using to replace the string(s) with an empty string:
$Path = "C:\some.txt"
$Content = [System.IO.File]::ReadAllLines($Path)
foreach ($string in (Get-Content "C:\strings_i_wish_to_remove.txt"))
{
$Content = $Content -replace $string, ""
}
$Content | Set-Content -Path $Path
And this code snippet works flawlessly with replacing all the words I wish to remove with an empty string; but the issue is that instead of completely removing the existence of the element it's now an empty string which is still processed.
Data before I added the replacement portion:
"Data title", "value 1", "value 2"
"Data title", "value 1", "value 2"
"Data title", "value 1", "value 2"
"SECTION HEADER", "Data title", "value 1"
"value 2", "Data title", "value 1"
"value 2", "Data title", "value 1"
"value 2", "Data title", "value 1"
Data after I added the replacement portion:
"Data title", "value 1", "value 2"
"Data title", "value 1", "value 2"
"Data title", "value 1", "value 2"
"", "Data title", "value 1"
"value 2", "Data title", "value 1"
"value 2", "Data title", "value 1"
"value 2", "Data title", "value 1"
Pictured above is the end result of my data after all the processing is complete; and as you can see, if the "SECTION HEADER" was entirely removed it would be aligned properly without any offsetting...
Therefore, is it possible to completely remove the desired word(s) from the file without having any leftover remnants of it still interfering with the subsequent processing steps?
Any and all help is greatly appreciated
CodePudding user response:
This is happening because it is treating "" also as a string and not special char or symbol, you need to include that as well in replace statement, e.g.
`
$Path = "d:\some.txt"
$Content = [System.IO.File]::ReadAllLines($Path)
$list=Get-Content "d:\strings_i_wish_to_remove.txt"
foreach ($string in $list)
{
$Content = $Content -replace $string, $null
}
//added line below to replace extra text.
$Content= $Content -replace '"",', $null
$Content | Set-Content -Path $Path
`