且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

[批次]如何使用CMD / PowerShell删除不需要的文本

更新时间:2022-10-16 08:12:39

我喜欢前面的答案,但想向您展示一个更简单的方法,如果您可以安装WMF / PowerShell版本5。



此版本增加了超级强大的功能新工具 ConvertFrom-String



如果要编写一些 really ,则使用FlashExtract(可在Excel中找到)和输入示例文件从任何给定的输入中智能地提取值。 em>简洁的代码,您可以使用这种方法。要创建输入示例文件,只需将您的预期输出复制并粘贴到记事本中即可。

 当前查询集
名称: srv-comms-xx;
名称:easrv00xxx;
名称:ealxsrv00xxx;

接下来,记下您要选择的值(在本例中为服务器名称)。现在,对于列表中的前几个服务器名称,请使用大括号,然后将您要使用的名称称为提取的值,然后在所需结尾处使用大括号。这是一个示例。

 当前查询集
名称:{name *:srv-comms-xx};
名称:{name *:easrv00xxx};
名称:ealxsrv00xxx;

请注意*,它表示每次看到此值时,ConvertFrom-String都会创建新对象。



所以,这就是我们的示例文件。现在将其保存在某个地方(在我的示例中为T:\Example.txt),然后使用Convertfrom-String调用它。

 获取-Content .\output.txt | ConvertFrom-String -TemplateFile T:\example.txt 

输出

  name 
----
srv-comms-xx
easrv00xxx
ealxsrv00xxx

请注意,我们仅提供了两个示例,但是PowerShell足够聪明,可以识别模式并提取最终服务器名称。即使我们的输入有成千上万个名称,我们也只需要前三到四行的示例就可以为PowerShell提供足够的提示来提示我们要做什么。



如果您要抓取文件并仅返回服务器名称,只需将其输入选择-扩展名称,如下所示:

 获取内容.\output.txt | ConvertFrom-String -TemplateFile T:\example.txt | 
选择对象-扩展属性名称


I've been searching for a while now for a solution to this problem: This is the content from the beginning of the textfile output.txt (ofc there follow many more lines of servernames):

Current query set
                    name: srv-comms-xx;
                    name: easrv00xxx;
                    name: ealxsrv00xxx;

I would need a batch script that:

  • removes the first line of this file

  • removes everything from other lines except the servernames

So output of example would be:

srv-comms-xx
easrv00xxx
ealxsrv00xxx

I expiremented with findstr but without success.

I like the previous answers, but wanted to show you an easier way available, if you can install WMF/PowerShell version 5.

This release adds a super powerful new tool called ConvertFrom-String. This uses FlashExtract (found in Excel) and an input example file to intelligently pull out values from any given input.

If you wanted to write some really succinct code, you could use this approach. To make an input example file, just copy and paste your expected output into notepad.

Current query set
                name: srv-comms-xx;
                name: easrv00xxx;
                name: ealxsrv00xxx;

Next, note the values you want to select, (in our case, the server names). Now, for the first few server names in the list put curly braces, the name you'd like to call this extracted value, and then a close brace at the end of what you want. Here's an example.

Current query set
                    name: {name*:srv-comms-xx};
                    name: {name*:easrv00xxx};
                    name: ealxsrv00xxx;

Note the *, which tells ConvertFrom-String to make new objects everytime we see this value.

So, that's our example file. Now save it somewhere (in my example T:\Example.txt), and call it using Convertfrom-String.

Get-Content .\output.txt | ConvertFrom-String -TemplateFile T:\example.txt

Output

name        
----        
srv-comms-xx
easrv00xxx  
ealxsrv00xxx

Note that we only provided two examples, but PowerShell was smart enough to recognize the pattern and extract the final server name too. Even if our input had thousands of names, we'd only need an example of the first three or four lines to give PowerShell enough hints as to what we want to do.

If you wanted to scrape the file and ONLY return the server names, just pipe into Select -Expand Name, like this:

Get-Content .\output.txt | ConvertFrom-String -TemplateFile T:\example.txt |
  Select-Object -ExpandProperty Name