从文本文件中删除行,如果它以 [英] Remove lines from text file if it starts with

查看:44
本文介绍了从文本文件中删除行,如果它以的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有包含无效记录的大型文本文件.

I have large text files that contain invalid records.

我想删除第一个字段为空的所有行.文件分隔符是波浪号,所以基本上我想删除第一个字符是 ~

I would like to remove all lines where the first field is blank. The file delimiter is a tilde , so essentially I want to remove all lines where the first character is ~

有人可以帮忙编写 PowerShell 代码吗?我似乎做对了.

Can someone assist with the PowerShell code - I cannot seem to get it right.

推荐答案

只需将我评论中的答案部分添加为答案,以便人们可以看到问题已得到解答.

Just adding the answer part from my comment as an answer, so that people can see that the question has been answered.

提出问题的人的解决方案是:

The solution which worked from the person asking the question was:

Get-Content file.txt | Where { $_ -notmatch "^~" } | Set-Content filteredfile.txt

请注意,注意潜在的编码问题.如果您需要指定编码,Get-ContentSet-Content 方法都有一个 -Encoding 参数,您可以使用它来指定编码.

As a note, be aware of potential encoding issues. If you need to specify encoding, both the Get-Content and the Set-Content methods have an -Encoding parameter which you can use to specify encoding.

这篇关于从文本文件中删除行,如果它以的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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