使用 PowerShell 从包含字符串的文本文件中删除行 [英] Using PowerShell to remove lines from a text file if it contains a string

查看:36
本文介绍了使用 PowerShell 从包含字符串的文本文件中删除行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下 PowerShell 代码从包含部分字符串的文本文件中删除所有行:

I am trying to remove all the lines from a text file that contains a partial string using the below PowerShell code:

 Get-Content C:\new\temp_*.txt | Select-String -pattern "H|159" -notmatch | Out-File C:\new\newfile.txt

实际字符串是 H|159|28-05-2005|508|xxx,它在文件中重复多次,我试图只匹配上面指定的第一部分.那是对的吗?目前我的输出为空.

The actual string is H|159|28-05-2005|508|xxx, it repeats in the file multiple times, and I am trying to match only the first part as specified above. Is that correct? Currently I am getting empty as output.

我错过了什么吗?

推荐答案

Escape the |使用反引号的字符

Escape the | character using a backtick

get-content c:\new\temp_*.txt | select-string -pattern 'H`|159' -notmatch | Out-File c:\new\newfile.txt

这篇关于使用 PowerShell 从包含字符串的文本文件中删除行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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