格式化操作符 -F 在 PowerShell 文本操作符中非常重要, 经常被用来增强数字类型和日期类型的可读性:
- "{0} diskettes per CD" -f (720mb/1.44mb)
- 500 diskettes per CD
所有的基本操作符形式都大同小异, 要处理的数据位于操作符的左右两边, 然后通过操作符建立连接. 例如, 你可以使用下面的语句将文本中指定的字符串替换成目标文本:
- "Hello Carl" -replace "Carl", "Eddie"
- Hello Eddie
-replace 操作符有三种实现方式, 其它文本操作符也类似地有三种实现方式, 像 - replace,-ireplace,-creplace,i 前缀表示字符串大小写不敏感 (insensitive),c 前缀表示字符串大小写敏感 (case sensitive).
- # 下面的例子没有完成替换, 因为当前大小写敏感:
- "Hello Carl" -creplace "carl", "eddie"
- Hello Carl
第三类 i 前缀, 表示大小写不敏感, 和没有前缀的命令逻辑一样 (PowerShell 中默认的字符串比较是不区分大小写的, 所以这里保持一致).
字符串操作符 | ||
操作符 | 描述 | 示例 |
* | 代表一个字符串 | “PsTips.Net” -like “*” |
+ | 合并两个字符串 | “Power” + “Shell” |
-replace,-ireplace | 替换字符串,大小写不敏感 | “PsTips.Net” -replace “tip”,”1″ |
-creplace | 替换字符串,大小写敏感 | “PsTips.Net” -replace “Tip”,”1″ |
-eq, -ieq | 验证是否相等,大小写不敏感 | “Power” -eq “power” |
-ceq | 验证是否相等,大小写敏感 | “Power” -eq “Power” |
-like, -ilike | 验证字符串包含关系,允许模式匹配,大小写不敏感 | “PsTips.Net” -like “p*” |
-clike | 验证字符串包含关系,允许模式匹配,大小写敏感 | “PsTips.Net” – clike “P*” |
-notlike, -inotlike | 验证字符串不包含关系,允许模式匹配,大小写不敏感 | “PowerShell” -notlike “PS*” |
-cnotlike | 验证字符串不包含关系,允许模式匹配,大小写敏感 | “PowerShell” -cnotlike “PO*” |
-match,-imatch | 验证模式匹配,大小写不敏感 | “PowerShell” -match “P*” |
-cmatch | 验证模式匹配,大小写敏感 | “Hello” -match “[ao]” |
-notmatch, -inotmatch | 验证模式不匹配,大小写不敏感 | “Hello” -notmatch “[ao]” |
-cnotmatch | 验证模式不匹配,大小写敏感 | “Hello” -cnotmatch “[ao]” |
PowerShell 使用特殊文本命令 -- 格式化字符串
格式化操作符 -F 能够将一个字符串格式化为指定格式, 左边是包含通配符的字符串, 右边是待插入和替换的字符串.
- "{0} diskettes per CD" -f (720mb/1.44mb)
- 500 diskettes per CD
-F 右边的表达式必选放在圆括号中, 作为一个整体, 先进行计算, 然后在格式化. 否则可能会解析错误:
PS > "{0} diskettes per CD" -f 720mb/1.44mb
数字常量无效: 754974720 diskettes per CD.
所在位置 行: 1 字符: 33
- + "{0} diskettes per CD" -f 720mb/ <<<< 1.44mb
- + CategoryInfo : InvalidOperation: (754974720 diskettes per CD:String) [], RuntimeException
- + FullyQualifiedErrorId : BadNumericConstant
可以在 - F 的左边放置多个字符串通配符, 类似. NET 中的 String.Format 方法.-F 右边相应的值或表达式也须要使用逗号分隔.
- "{0} {3} at {2}MB fit into one CD at {1}MB" -f (720mb/1.44mb), 720, 1.44, "diskettes"
- 500 diskettes at 1.44MB fit into one CD at 720MB
PowerShell 使用特殊文本命令 -- 设置数字格式
格式化操作符 -f 可以将数值插入到字符串, 每一个通配符都有统一的结构.
- {
- index[,alignment][:format]
- }:
- Index:
索引编号用来识别把那个值用来替换通配符. 例如你可能使用了多个通配符, 或者同一个通配符使用了多次, 甚至多种格式. 此时, 索引编号是唯一能够识别那个值将用来替换. 另外两个选项 Alignment 和 Format 则作为辅助条件.
Alignment:
正数和负数, 可以指定目标值是否左对齐或者右对齐. 还可以支持数值以指定的宽度显示, 如果数值的实际宽度大于指定宽度, 则忽略指定宽度. 如果数值宽度小于指定宽度, 剩余的部分会以空白填充, 这一选项非常利于制表.
Format:
数值可以被格式化成许多不同的类型, 下面会预览这些你可能会用到一些格式概要.
格式化语句比较特殊, 大小写敏感, 这和 PowerShell 中其它语句的使用稍有不同. 下面举个例子:
- # 使用小写字母 d: 格式化
- "Date: {0:d}" -f (Get-Date)
- Date: 2013/5/31
- # 使用大写字母 D: 格式化
- "Date: {0:D}" -f (Get-Date)
Date: 2013 年 5 月 31
符号 | 类型 | 调用示例 | 输入结果 |
# | 数字占位符 | “{0:(#).##}” -f $value | (1000000) |
% | 百分号 | “{0:0%}” -f $value | 100000000% |
, | 千分符 | “{0:0,0}” -f $value | 1,000,000 |
,. | 一千的整数倍 | “{0:0,.} ” -f $value | 1000 |
. | 小数点 | “{0:0.0}” -f $value | 1000000.0 |
0 | 占位符 0 | “{0:00.0000}” -f $value | 1000000.0000 |
c | 货币 | “{0:c}” -f $value | 1,000.00 |
d | 十进制 | “{0:d}” -f $value | 1000000 |
e | 科学计数法 | “{0:e}” -f $value | 1.000000e+006 |
e | 指数通配符 | “{0:00e+0}” -f $value | 10e+5 |
f | 保留小数位 | “{0:f}” -f $value | 1000000.00 |
g | 常规 | “{0:g}” -f $value | 1000000 |
n | 千分符 | “{0:n}” -f $value | 1,000,000.00 |
x | 十六进制 | “0x{0:x4}” -f $value | 0x4240 |
使用上面表格中, 你可以快速并舒服地格式化数值, 例如千分符的使用可以让用户避免去纠结, 1 后面到底跟了几个 0, 是 10 万呢, 还是 100 万.
另外 PowerShell 还提供了非常丰富的日期格式化选项, 相关的格式见下表.
符号 | 类型 | 调用示例 | 输出 |
d | 短日期格式 | “{0:d}” –f $value | 2013/6/1 |
D | 长日期格式 | “{0:D}” –f $value |
- $date= Get-Date
- Foreach ($format in "d","D","f","F","g","G","m","r","s","t","T", `
- "u","U","y","dddd, MMMM dd yyyy","M/yy","dd-MM-yy") {
- "PowerShell 日期格式, 使用 $format : {0}" -f $date.ToString($format)
- }
- [appdomain]::currentdomain.getassemblies() | ForEach-Object {
- $_.GetExportedTypes() | Where-Object {
- ! $_.IsSubclassof([System.Enum])
- }
- } | ForEach-Object {
- $Methods = $_.getmethods() | Where-Object {
- $_.name -eq "tostring"
- } |%{
- "$_"
- };
- If ($methods -eq "System.String ToString(System.String)") {
- $_.fullname
- }
- }
- System.Enum
- System.DateTime
- System.Byte
- System.Convert
- System.Decimal
- System.Double
- System.Guid
- System.Int16
- System.Int32
- System.Int64
- System.IntPtr
- System.SByte
- System.Single
- System.UInt16
- System.UInt32
- System.UInt64
- Microsoft.PowerShell.Commands.MatchInfo
- PS > $guid = [GUID]::NewGUID()
- PS > Foreach ($format in "N","D","B","P") {
- >> "GUID with $format : {0}" -f $GUID.ToString($format)
- }
- >>
- GUID with N : e1a5d98f4227470b84c2b37a6a8fb894
- GUID with D : e1a5d98f-4227-470b-84c2-b37a6a8fb894
- GUID with B : {
- e1a5d98f-4227-470b-84c2-b37a6a8fb894
- }
- GUID with P : (e1a5d98f-4227-470b-84c2-b37a6a8fb894)
- PS > dir | ForEach-Object {
- "$($_.name) = $($_.Length) Bytes"
- }
- .Android = Bytes
- .VirtualBox = Bytes
- CMB = Bytes
- Contacts = Bytes
- Desktop = Bytes
- Documents = Bytes
- Downloads = Bytes
- Favorites = Bytes
- funshion = Bytes
- Links = Bytes
- Podcasts = Bytes
- Roaming = Bytes
- Saved Games = Bytes
- Searches = Bytes
- SkyDrive = Bytes
- Tracing = Bytes
- Virtual Machines = Bytes
- VirtualBox VMs = Bytes
- a = 12022 Bytes
- a.CSV = 986 Bytes
- a.ps1 = 18 Bytes
- a.txt = 946 Bytes
- funshion.INI = 6798 Bytes
- PUTTY.RND = 600 Bytes
- PS> dir | ForEach-Object {
- "{0,-20} = {1,10} Bytes" -f $_.name, $_.Length
- }
- Virtual Machines = Bytes
- VirtualBox VMs = Bytes
- a = 12022 Bytes
- a.CSV = 986 Bytes
- a.ps1 = 18 Bytes
- a.txt = 946 Bytes
- funshion.INI = 6798 Bytes
- PUTTY.RND = 600 Bytes
- .....
- PS> $path = "C:\prefs.js"
- PS> $path.Substring( $path.LastIndexOf(".")+1 )
- JS
- PS> $path.Split(".")[-1]
- JS
- PS C:\> ("Pstips.net" | Get-Member Split).definition
- string[] Split(Params char[] separator), string[] Split(char[] separator, int count), string[] Split(char[] separator, System.StringSplitOptions options), string[] Split(char[] separator, int count, System.StringSplitOptions options), string[] Split(string[] separator, System.StringSplitOptions options), string[] Split(string[] sepa
- rator, int count, System.StringSplitOptions options)
- PS C:\> ("Pstips.net" | Get-Member Split).definition.Replace("),", ")`n")
- string[] Split(Params char[] separator)
- string[] Split(char[] separator, int count)
- string[] Split(char[] separator, System.StringSplitOptions options)
- string[] Split(char[] separator, int count, System.StringSplitOptions options)
- string[] Split(string[] separator, System.StringSplitOptions options)
- string[] Split(string[] separator, int count, System.StringSplitOptions options)
- PS C:\> "http://www.pstips.net".split(":./")
- http
- www
- pstips
- net
- PS C:\> "http://www.pstips.net".split(":./",[StringSplitOptions]::RemoveEmptyEntries)
- http
- www
- pstips
- net
- function RemoveSpace([string]$text) {
- $private:array = $text.Split(" ", `
- [StringSplitOptions]::RemoveEmptyEntries)
- [string]::Join(" ", $array)
- }
- PS C:\> RemoveSpace("PowerShell 中文博客的网址为 :http://www.pstips.net")
- PS C:\> "Hello" + "" +"World!"
- Hello World!
- + (Get-Date) + <<<< "PStips.Net"
- + CategoryInfo : NotSpecified: (:) [], MethodException
- + FullyQualifiedErrorId : MethodArgumentConversionInvalidCastArgument
- PS C:\> ""+(Get-Date) +"PStips.Net"
- 06/04/2013 00:54:27PStips.NET
- PS C:\> [string]::Concat((Get-Date),"PStips.Net")
- 2013/6/4 0:55:00PStips.NET
- # 列出当前目录中的文本文件
- Dir *.txt
- # 列出系统目录中以'n'或'w'打头的文件
- dir $env:windir\[nw]*.*
- # 列出文件后缀名以't'打头, 并且后缀名只有三个字符的文件
- Dir *.t??
- # 列出文件中包含一个'e'到'z'之间任意字符的文件
- dir *[e-z].*
- $ip = Read-Host "IP address"
- If ($ip -like "*.*.*.*") {
- "valid"
- } Else {
- "invalid"
- }
- $email = "[email protected]"
- $email -like "*.*@*.*"
来源: http://www.bubuko.com/infodetail-3275879.html