获取帮助
sunzhengbo in Desktop ❯ get-help lsNAMEGet-ChildItemSYNOPSISGets the items and child items in one or more specified locations.SYNTAXGet-ChildItem [[-Filter] <System.String>] [-Attributes {Archive | Compressed | Device | Directory | Encrypted | Hidden | IntegrityStream | Normal | NoScrubData | NotContentIndexed | Offline | ReadOnly | ReparsePoint | SparseFile |System | Temporary}] [-Depth <System.UInt32>] [-Directory] [-Exclude <System.String[]>] [-File] [-FollowSymlink] [-Force] [-Hidden] [-Include <System.String[]>] -LiteralPath <System.String[]> [-Name] [-ReadOnly] [-Recurse] [-System] [<CommonParameters>]
过滤
过滤对象(where-object)
获取 % 的别名信息
sunzhengbo in Desktop ❯ get-alias | where-object Name -eq %CommandType Name Version Source----------- ---- ------- ------Alias % -> ForEach-Object
获取 % 和 ? 的别名信息
sunzhengbo in Desktop ❯ get-alias | where-object Name -in %, ?CommandType Name Version Source----------- ---- ------- ------Alias ? -> Where-ObjectAlias % -> ForEach-Object
过滤文本(select-string)
测试的文本
sunzhengbo in Desktop ❯ cat .\aa.txtImport-Module posh-gitImport-Module oh-my-poshSet-PoshPrompt -Theme spaceship
将行中包含某个字符的行过滤出来
sunzhengbo in Desktop ❯ cat .\aa.txt | select-string importImport-Module posh-gitImport-Module oh-my-posh
显示文本的第一列
sunzhengbo in Desktop ❯ cat .\aa.txt | select-string import | %{($_ -split ' ')[0]}Import-ModuleImport-Module
显示文本的第后列
sunzhengbo in Desktop ❯ cat .\aa.txt | select-string import | %{($_ -split ' ')[-1]}posh-gitoh-my-posh
显示文本的1-2列
sunzhengbo in Desktop ❯ cat .\aa.txt | %{"$(($_ -split ' ')[0..1])"}Import-Module posh-gitImport-Module oh-my-poshSet-PoshPrompt -Theme
显示文本的第1列,第2列
sunzhengbo in Desktop ❯ cat .\aa.txt | %{"$(($_ -split ' ')[0,1])"}Import-Module posh-gitImport-Module oh-my-poshSet-PoshPrompt -Theme
文本根据多个条件拆分
关于 .. 运算符,有一点需要注意。 序列 0..-1 和 -1..0 的计算结果为值 0,-1 和 -1,0。 如果忘记了这一细节,就很容易看到 $data[0..-1],并认为它会枚举所有项
sunzhengbo in Desktop ❯ cat .\aa.txt | %{"$(($_ -split {$_ -eq ' ' -or $_ -eq '-'})[0..2])"}Import Module poshImport Module ohSet PoshPrompt
