无法在Powershell中转义管道字符(|) [英] Unable to escape pipe character (|) in powershell

查看:150
本文介绍了无法在Powershell中转义管道字符(|)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在文件的每一行中找到管道(|)字符的数量。我正在使用以下命令来执行此操作。

I am trying to find number of pipe(|) characters in each line of a file. I am using following command to do so.

gc .\test.txt | % { ($_ | select-string "|" -all).matches | measure | select count }

不计算管道符号。

我也尝试过


  1. `|

  2. '|'

  3. ||

有人可以告诉我如何在电源外壳中转义管道字符吗?

Can anyone tell me how to escape pipe character in power shell?

如果我使用的不是管道字符或其他字符,则命令运行正常。

If i am using strings or characters other than pipe the command is working properly.

推荐答案

A反斜杠 \ 是正则表达式的转义字符。

A backslash \ is the escape character for a regular expression.

gc .\test.txt | % { ($_ | select-string "\|" -all).matches | measure | select count }

如果不确定,可以随时使用 [RegEx ] :: Escape()

If you're unsure, you can always use [RegEx]::Escape():

$pattern = [RegEx]::Escape("|")
gc .\test.txt | % { ($_ | select-string $pattern -all).matches | measure | select count }

否则,不必在PowerShell中将管道转义为字符串。

The pipe otherwise does not have to be escaped in PowerShell inside a string.

这篇关于无法在Powershell中转义管道字符(|)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆