I am trying to clean up some text files from multiple spaces and other characters. I want only the text within double quotes to remain in the line.
Here is the example of the text file:
"uid" : "Text To Remain", "id" : "Text2 To Stay",
Note the empty characters / tabs at the beginning of each line and comma at the end of each line.
So I thought the easiest way to get rid of those empty spaces on the left would be using the regular expression. In every line there is a space_colon_space string: " : ", so I try to erase everything to the left of it, including the string itself.
I came up with two examples of solutions:
get-content 'K:\text.txt' -ReadCount 1000 |
ForEach-Object {
$_.replace(".* : ", "").replace(",", "")
} |
Out-File 'K:\text_cleaned.txt'
This solution works only for the comma, but does not work for the colon. There is no error.
Second solution:
get-content 'K:\text.txt' -ReadCount 1000 |
foreach { $_ -replace ".* : " | out-file 'K:\text_cleaned.txt'
}
And this works and cleans up everything on the left of the first double quote character, but I have no idea how to add a function to replace the comma at the end of each line in the same line.
Why not to do it in a simpler way?
I am very curious why the regular expression /.* : / in the first solution does not work, while the one in the second does work. And there is no error in the first one.
Could you enlighten me?