【问题标题】:Reason for twice the selection in a select-string command选择字符串命令中两次选择的原因
【发布时间】:2019-12-22 17:48:39
【问题描述】:

这个脚本,

$lt="_cdbc ","_dié","_diéq ","_cost ","_coste "
foreach($x in $lt){sls -path GA0.txt -pattern $x -CaseSensitive >> GA1.txt}

正在处理文件GA0的以下内容(状态栏中的编码标签:utf-8),

1 _cdbc \> contenu  diaphane\\
2 _dié \> cable deux\\
3 _diéq \> vingt \\
4 _cost \> pin parasol\\
5 _coste \> thyme\\

在 GA1 中产生以下结果(在状态栏中产生编码标签,无论初始标签(utf-8 或 utf-16 Le):utf-16 Le),

1                                        <emty line>
2  GA0.txt:1:_cdbc \> contenu  diaphane\\
3                                        <emty line>
4                                        <emty line>
5                                        <emty line>
6  GA0.txt:2:_dié \> cable deux\\
7  GA0.txt:3:_diéq \> vingt \\
8                                        <emty line>
9                                        <emty line>
10                                       <emty line>
11 GA0.txt:3:_diéq \> vingt \\
12                                       <emty line>
13                                       <emty line>
14                                       <emty line>
15 GA0.txt:4:_cost \> pin parasol\\
16                                       <emty line>
17                                       <emty line>
18                                       <emty line>
19 GA0.txt:5:_coste \> thyme\\

1/ 很明显,但不确定第 3 行(第 7 行和第 11 行)列出两次的问题是由重音字母 (é) 引起的:例如,模式没有错误“成本/成本”。我尝试添加参数规范-encoding utf8 并没有什么区别。有人知道该怎么做才能做到这一点吗?
2/ 除了问题行之前,GA1 中结果行之间的第一个空行和一系列 3 个空行是什么原因?如何更改代码以使列表没有任何空行?

【问题讨论】:

  • 关于第二点,Select-String cmdlet 返回一个 MatchInfo 对象。如果您专门选择 line 属性,您将获得所需的结果。 foreach($x in $lt){sls -path GA0.txt -pattern $x -CaseSensitive | % {$_.Line}&gt;&gt; GA1.txt}
  • 该问题有一个难以理解的错误,这使得它仅对第二部分(/2)有效。 ' "_dié" '应该是' "_dié " '。

标签: windows powershell select-string


【解决方案1】:

&gt;&gt;| out-file -append 添加额外的格式(格式自定义)。请改用| add-content"_dié" 后面没有空格,所以它匹配两行。由于"_cost "末尾有空格,所以只匹配一行。

您也可以一次完成所有模式:

search-string $lt da0.txt

对于整个单词匹配,您需要一点额外的正则表达式:Select-String -pattern wholeword

【讨论】:

    猜你喜欢
    • 2011-11-14
    • 2013-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多