windows - 在选择字符串命令中两次选择的原因

标签 windows powershell select-string

这个脚本

$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行)的问题是由带重音的字母(é)引起的:例如,“cost /哥”。我尝试添加参数规范-encoding utf8,但没有任何区别。有人知道该怎么做吗?
2 /除了有问题的行之前,GA1中结果行之间的第一个空行和3个空行系列的原因是什么?如何更改代码以使列表中没有任何空行?

最佳答案

>>| out-file -append添加了额外的格式设置(format-custom)。请尝试| add-content"_dié"后没有空格,因此它匹配两行。由于"_cost "在结尾处有空格,因此它仅匹配一行。

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

search-string $lt da0.txt

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

关于windows - 在选择字符串命令中两次选择的原因,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59446670/

相关文章:

powershell - 创建运行批处理文件的快捷方式

powershell - 从输出中删除路径

windows - %M2% 在系统变量 Path 中不起作用,但仅在用户变量 Path 中起作用

windows - Clojure 入门

windows - 本地安装的 Helvetica Neue 在 Windows 浏览器中的显示效果与 Mac OS X 上的不同。为什么?

windows - NW.js Windows 10 任务栏图标始终显示默认值

powershell - 如何更改 Windows 控制台 CMD 或 WSL 中的键盘布局?

powershell - 如何根据过滤器选择特定的子串长度

powershell - 如果将字符串放在一个序列或另一个序列中,为什么使用Powershell 4.0在文本文件中搜索字符串会被忽略?

powershell 选择字符串无法正常工作