简体   繁体   English

Powershell删除文本文件中的字符串

[英]Powershell Delete String in text file

I have a file that I am downloading daily what I want to do is create a script that reads the text file and deletes out any lines containing the string "---" 我有一个每天要下载的文件,我想要做的是创建一个脚本,该脚本读取文本文件并删除包含字符串“ ---”的所有行。

I started with "(get-content L:\\holdings.txt) | where {$_ -ne "" | out-file holdings.txt" 我从"(get-content L:\\holdings.txt) | where {$_ -ne "" | out-file holdings.txt"

This deletes all the blank lines without any issue, I then tried 这将删除所有空白行,没有任何问题,然后我尝试了

$del -like "*-*"
"(get-content L:\holdings.txt) | where {$_ -ne $del | out-file L:\holdings.txt"

Which I though would delete out any of the lines of data, whilst it runs without error the line is not being deleted out... 虽然我会删除任何数据行,但在运行时没有错误,该行并未被删除...

I don't know if it's relevant but the line I want to delete will always be the second line as it is a comma delimited SQL batch file automation. 我不知道它是否相关,但是我要删除的行始终是第二行,因为它是逗号分隔的SQL批处理文件自动化。

The file reads as follows 文件内容如下

Advice Num ,Bargain Number 咨询编号,议价编号
--------------------,----------------- --------------------,-----------------

Just to try and give you a graphical representation that I am trying to resolve 只是为了给您一个我要解决的图形表示

Any ideas? 有任何想法吗?

Regards 问候

Richard 理查德

Solved it, a quick amendment and here it is (get-content "L:\\movement.txt") -notmatch "---" | out-file "L:\\movement.txt" 解决了这个问题,一个快速修正,这里是(get-content "L:\\movement.txt") -notmatch "---" | out-file "L:\\movement.txt" (get-content "L:\\movement.txt") -notmatch "---" | out-file "L:\\movement.txt"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM