【问题标题】:Powershell file row starts in 'String' and ends in 'Integer'Powershell 文件行以“字符串”开头并以“整数”结尾
【发布时间】:2020-07-17 03:52:48
【问题描述】:

我正在读取一个文本文件,如果一行以字符串开头并以数字结尾,我需要在其中过滤并执行一些格式化。如果我知道/修复了值,我可以使用 StartWith 或 EndsWith 来过滤这一行。 我尝试如下使用它,但我猜这是错误的,因为它不起作用。

if(($record.StartsWith.GetType() -eq [string]) -and  ($record.EndsWith.GetType() -eq [int]))

示例:我的行已读

TransDate,Merchant,Amount
Doe,Jane, *6098
3/4/2020,Walmart,6.45
3/4/2020,Starbucks,14.45
Doe,John, *6096
3/4/2020,Amazon,34.45

我需要确定第 2 行和第 5 行有我的卡号。如何在 powershell 中识别这些?请告诉我。谢谢

【问题讨论】:

    标签: powershell startswith ends-with


    【解决方案1】:

    您可以使用正则表达式来识别您所追求的行...一个简单的版本可能如下所示:

    $Content = Get-Content D:\sample\TextFile.txt
    foreach($record in $Content){
        if($record -match '^[a-z].*\d$'){
            $record
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-13
      • 1970-01-01
      • 2019-07-20
      • 1970-01-01
      • 2021-11-24
      • 2011-11-02
      相关资源
      最近更新 更多