使用PowerShell来清理文本文件

一直在努力,并想知道如果有人可以帮助。 我有一个大的文本文件,其中有额外的数据,我想剥离。 以下是input文件的示例:

Text In Page - 1 S Dept l<m RKB) "1915 slightly 234234 "sil dsf 56 "gr gl 1920 100 1925 100 1930 100 Cls "1935 100 Cl Text In Page - 2 l<m RKB) "1915 slightly "sil "gr glauc 1920 100 1925 100 1930 100 Cls "1935 100 Cl 

我想删除以下内容:

  • 任何空白行
  • 任何“在行的开头
  • 任何以字母AZ,az开头的行

因此,以上面的例子,我会留下

 1915 1920 100 1925 100 1930 100 Cls 1935 100 Cl 1915 1920 100 1925 100 1930 100 Cls 1935 100 Cl 

我在想:

 (gc D:\test.txt) -replace '^"' | sls '\S' | sls -NotMatch '^[A-Za-z]' | sc out.txt 

其中:

  • 获取文件的行,如果第一个字符是引号,则将其replace为空格
  • select匹配“不是空白”的行(即空行被过滤掉)
  • select不以A-Za-z开始的行
  • 将结果写入out.txt

有很多种方式来编写长版本,这取决于你喜欢用pipe道链接多less东西,而不是一遍又一遍地处理variables,但是它是这样做的:

 $lines = Get-Content D:\test.txt $lines = $lines -replace '^"' $lines = $lines | Select-String '\S' $lines = $lines | Select-String -NotMatch '^[A-Za-z]' $lines | Set-Content out.txt