0
点赞
收藏
分享

微信扫一扫

powershell_格式化字符串(composite formating)/windows的类似cat -n(查看文件显示行号)/powershell查看文件指定范围的行


文章目录

  • ​​powershell_格式化字符串(composite formating)/windows的类似cat -n(查看文件显示行号)/powershell查看文件指定范围的行​​
  • ​​字符串和操作符​​
  • ​​powershell字符串对齐(Using the alignment component)​​
  • ​​catn​​
  • ​​windows的cat -n(查看文件并显示行号)​​
  • ​​管道符版catn​​
  • ​​结合catn查看​​


powershell_格式化字符串(composite formating)/windows的类似cat -n(查看文件显示行号)/powershell查看文件指定范围的行

  • ​​Use PowerShell to Format Strings with Composite Formatting - Scripting Blog (microsoft.com)​​
  • MSDN:​​Composite formatting | Microsoft Docs​​
  • ​​Understanding PowerShell and Basic String Formatting - Scripting Blog (microsoft.com)​​
  • ​​command line - Windows equivalent to Linux ​​cat -n​​? - Super User​​
  • ​​Creating Pipeline-Aware Functions - powershell.one​​

字符串和操作符

  • ​​about Operators - PowerShell | Microsoft Docs​​

Format operator ​​-f​

  • Formats strings by using the format method of string objects.
  • Enter the format string on the left side of the operator and the objects to be formatted on the right side of the operator.
    powershell_格式化字符串(composite formating)/windows的类似cat -n(查看文件显示行号)/powershell查看文件指定范围的行_microsoft
    powershell_格式化字符串(composite formating)/windows的类似cat -n(查看文件显示行号)/powershell查看文件指定范围的行_windows_02

powershell字符串对齐(Using the alignment component)

  • To provide for the formatting of output within “cells,” I use the alignment component of the format item.
  • The alignment component is optional, as seen in the three earlier examples that did not include the alignment component.
  • The alignment is​​a signed integer​​​that specifies the preferred​​field width​​.
  • If the value of the alignment is smaller than​​the actual length of the formatted string​​​, the value of the alignment component​​is ignored​​.
  • Alignment to the right takes place if the value of the integer is positive. Alignment to the left takes place if the integer is negative. The following examples illustrate various ways of using the alignment component.
  • powershell_格式化字符串(composite formating)/windows的类似cat -n(查看文件显示行号)/powershell查看文件指定范围的行_powershell_03
![1646722436409.png](https://img-blog.csdnimg.cn/img_convert/24caa74bfbd50ef30f2da0b8811f233d.png)

## 查看指定范围的行(内容)

> 譬如,结合`sls`和`select`来查找网关;

```powershell
PS D:\repos\configs> ipconfig|sls gateway
InputStream:23: Default Gateway . . . . . . . . . : fe80::38c8:62ff:fe6c:e5c3%15
InputStream:32: Default Gateway . . . . . . . . . :

PS D:\repos\configs> ipconfig|select -Index (20..25)
IPv4 Address. . . . . . . . . . . : 192.168.43.165
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Default Gateway . . . . . . . . . : fe80::38c8:62ff:fe6c:e5c3%15
192.168.43.1

Ethernet adapter vEthernet (Default Switch):

catn

windows的cat -n(查看文件并显示行号)

# 关键在于格式化'{0,5} {1}' -f
Get-Content $FileName | ForEach-Object { '{0,-5} {1}' -f $_.ReadCount, $_ }

管道符版catn

function catn
{
<#
.Synopsis
Mimic Unic / Linux tool nl number lines

.Description
Print file content with numbered lines no original nl options supported

.Example
nl .\food.txt
#>
param (
$Path = '',
[Parameter(ValueFromPipeline)]
[String]
$content
# $FileName

)
begin
{
$i = 0;
} process
{

if ($path -eq '')
{
$content | ForEach-Object {
$i++;
'{0,-5} {1} ' -f $i, $_ ;

}
}
else
{
# $content = (Get-Content $Path)
Get-Content $Path | ForEach-Object {
$i++;
'{0,-5} {1} ' -f $i, $_ ;

}
}
}
}

结合catn查看

  • 此版本支持管道符的用法
PS C:\Users\cxxu> ipconfig|select -Index (20..25)|catn
1 IPv4 Address. . . . . . . . . . . : 192.168.43.165
2 Subnet Mask . . . . . . . . . . . : 255.255.255.0
3 Default Gateway . . . . . . . . . : fe80::38c8:62ff:fe6c:e5c3%15
4 192.168.43.1
5
6 Ethernet adapter vEthernet (Default Switch):


举报

相关推荐

0 条评论