[批次]如何使用CMD / PowerShell删除不需要的文本 [英] [Batch ]How to remove unwanted text using CMD/PowerShell

查看:79
本文介绍了[批次]如何使用CMD / PowerShell删除不需要的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找解决此问题的方法:
这是文本文件 output.txt ( ofc后面还有很多行服务器名称):


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


我需要一个批处理脚本:




  • 删除此文件的第一行


  • 从其他行删除所有内容除了服务器名




所以示例输出为:



< blockquote>

  srv-comms-xx 
easrv00xxx
ealxsrv00xxx


我因findstr而过期,但没有成功。

解决方案

我喜欢前面的答案,但想向您展示一个更简单的方法,如果您可以安装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

这篇关于[批次]如何使用CMD / PowerShell删除不需要的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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