AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 1037391
Accepted
wirelessben
wirelessben
Asked: 2020-10-13 07:16:53 +0800 CST2020-10-13 07:16:53 +0800 CST 2020-10-13 07:16:53 +0800 CST

文件上的 Powershell Select-String 添加不需要的元数据

  • 772

我正在尝试在包含用户帐户的文件中过滤掉服务帐户,每行一个:

"svc.test"
"test.user"
"test.user2"

Select-String 添加不需要的元数据:

C:\Output> select-string -Path C:\Output\tmp.csv 'svc' -NotMatch   
tmp.csv:415:"test.user"  
tmp.csv:416:"test.user2"

我只是想:

"test.user"  
"test.user2"

我该怎么做呢?

--- 更新 --- 这是 Get-Member cmdlet 的结果:

 PS C:\Output> select-string -Path C:\Output\tmp.csv 'svc' -NotMatch|get-member


   TypeName: Microsoft.PowerShell.Commands.MatchInfo

Name         MemberType Definition                                                       
----         ---------- ----------                                                       
Equals       Method     bool Equals(System.Object obj)                                   
GetHashCode  Method     int GetHashCode()                                                
GetType      Method     type GetType()                                                   
RelativePath Method     string RelativePath(string directory)                            
ToString     Method     string ToString(), string ToString(string directory)             
Context      Property   Microsoft.PowerShell.Commands.MatchInfoContext Context {get;set;}
Filename     Property   string Filename {get;}                                           
IgnoreCase   Property   bool IgnoreCase {get;set;}                                       
Line         Property   string Line {get;set;}                                           
LineNumber   Property   int LineNumber {get;set;}                                        
Matches      Property   System.Text.RegularExpressions.Match[] Matches {get;set;}        
Path         Property   string Path {get;set;}                                           
Pattern      Property   string Pattern {get;set;}                                        
 

我需要数据。如何将线属性数据输出到文件?

powershell filter
  • 1 1 个回答
  • 531 Views

1 个回答

  • Voted
  1. Best Answer
    Lee_Dailey
    2020-10-13T10:18:41+08:002020-10-13T10:18:41+08:00

    这是做事的一种方法。[ grin ] Select-Stringcmdlet 始终包含大量元数据以及匹配项。对象类型是matchinfo,不是string,所以你需要得到你想要的值......在这种情况下,该值包含在.Line对象的属性中。

    代码的作用...

    • 设置要使用的文件名
    • 创建要排除的项目列表
    • 创建该列表
      的正则表达式 OR S-Scmdlet 默认使用-Pattern参数 for matches ... 并且使用正则表达式模式。
    • 创建一个要使用的文件
    • Select-String使用输入文件、正则表达式模式和-NotMatch用于反转匹配的 switch 参数运行
    • .Line获取每个对象的属性值
    • 将其发送到$Result集合
    • 在屏幕上显示该集合

    编码 ...

    $FileName = "$env:TEMP\wirelessben_S-S_Demo.txt"
    
    $ExcludeList = @(
        'line'
        'five'
        'seven'
        )
    $RegexEL = $ExcludeList -join '|'
    
    #region >>> create a file to work with
    @'
    the first line
    2nd line, this one is
    a third line
    the fourth goes forth
    one, two, five ... THREE sir!
    the number of this line is six
    seven
    eight
    number nine, number nine
    '@ | Set-Content -LiteralPath $FileName
    #endregion >>> create a file to work with
    
    
    $Result = (Select-String -LiteralPath $FileName -Pattern $RegexEL -NotMatch).Line
    
    $Result
    

    输出 ...

    the fourth goes forth
    eight
    number nine, number nine
    

    请注意,任何包含line、five或的行都seven被排除在外。

    • 0

相关问题

  • 资源锁和电源外壳

  • 脚本 - 如何断开远程桌面会话?

  • 如何限制向通讯组发送到外部地址?

  • Powershell对值与数组的作用不同?

  • Windows Powershell Vim 键绑定

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve