I need to add the quotation mark to a text file that contains 500 lines text. The format is inconsistent. It has dashes, dots, numbers, and letters. For example
1527c705-839a-4832-9118-54d4Bd6a0c89
16575getfireshot.com.FireShotCaptureWebpageScreens
3EA2211E.GestetnerDriverUtility
I have tried to code this
$Flist = Get-Content "$home\$user\appfiles\out.txt"
$Flist | %{$_ -replace '^(.*?)', '"'}
I got the result which only added to the beginning of a line.
"Microsoft.WinJS.2.0
The expected result should be
"Microsoft.WinJS.2.0"
How to add quotation-mark to the end of each line as well?
There is no strict need to use a regex (regular expression) in your case (requires PSv4+):
(Get-Content $home\$user\appfiles\out.txt).ForEach({ '"{0}"' -f $_ })
Array method .ForEach()
processes each input line via the script block ({ ... }
) passed to it.
'"{0}"' -f $_
effectively encloses each input line ($_
) in double quotes, via -f
, the string-format operator.
If you did want to use a regex:
(Get-Content $home\$user\appfiles\out.txt) -replace '^|$', '"'
Regex ^|$
matches both the start (^
) and the end ($
) of the input string and replaces both with a "
char., effectively enclosing the input string in double quotes.
As for what you tried:
^(.*?)
just matches the very start of the string (^
), and nothing else, given that .*?
- due to using the non-greedy duplication symbol ?
- matches nothing else.
Therefore, replacing what matched with "
only placed a "
at the start of the input string, not also at the end.