Strings and Text
PowerShell provides decent facilities for processing strings and text. You have full access to .NET libraries for strings and regular expressions. PowerShell also provides convenient facilities for dealing with multi-line strings and for string formatting. It also provides a nice facility for text localization.
Many years ago, I attempted to process very large files (10's of GBytes) with PowerShell. It was very slow and I have not tried again (I usually prefer to process them with another language, e.g. F#). I suppose that the speed improved over the years, but I do not see any compelling reason to do such processing in the shell.
Reading an entire file as a string
To read an entire file in a variable as a string, specify an invalid delimiter.
$content = Get-Content -Path $_.FullName -Delimiter 'doesnotexist'
Regular expressions
Examples of regular expressions:
$content = $content -ireplace '(?m)\^;;.*\n', ''
$content = $content -ireplace '(?m)^@COMMENT.*(\n|\Z)', ''
Observe the use of (?m)
. This changes the mode to match on multiple lines.
For more information, see: http://www.regular-expressions.info/powershell.html.
[TODO: Need to expand on regular expressions as it is a very challenging topic.
Formatting
It is often convenient to use a standard C# way for formatting text (similar to System.Console
). A nice way of doing so is using the -f
operator:
$world = "world!"
"{0} {1}" -f "Hello",$world
Standard format options are available, see Composite Formatting.
Resources
Some extra resources: