windows 如何使用批处理按字符串过滤txt文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2912952/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 14:31:59  来源:igfitidea点击:

How to filter txt file by string with using batch

windowsbatch-filedos

提问by Antediluvian

I know this kind of problem will be very easy for you but I am total beginner in batch mode.

我知道这种问题对你来说很容易,但我完全是批处理模式的初学者。

The question is how bat file code should look like to generate a new txt file from specific one .

问题是 bat 文件代码应该如何从特定的 .txt 文件生成新的 txt 文件。

for example .

例如 。

I have report txt

我有报告txt

Displaying status for license file: 7788@Server01

License Server: server01

                    License    In Use   Free
                    -------    ------   ----
                    Design*      1       6
                               (user1@host1)   127 server01 7788  4402

                  Assembly*      0       4

                       Pro       0      15

                     AdvSE       2       3
                              (user2@host2)  AdvSE server01 7788  2706
                              (user3@host3)  AdvSE server01 7788  1503

        SingleSite_License       1       3
                              (user4@host4)      SingleSite_License server01 7788  2003

      Intra_CLIENT_License        1       4
                              (user2@host2)      Intra_CLIENT_License server01 7788  2003

                       CAD       1      32
                              ^(user2@host2)     CAD server01 7788   501
* = License Extensions - Available only on startup.
^ = Borrowed License.
Press any key to continue . . . 

What I want to have in new file from this one are only lines :

我想在这个新文件中拥有的只有几行:

SingleSite_License 3

SingleSite_License 3

Intra_CLIENT_License 9

Intra_CLIENT_License 9

but both SingleSite_License and Intra_Client_License should be taken from 1st found string -other are not necessary.

但是 SingleSite_License 和 Intra_Client_License 都应该从第一个找到的字符串中获取 - 其他不是必需的。

report txt can be different and licenses can be showed in different order .

报告 txt 可以不同,许可证可以按不同的顺序显示。

if it not possible - other solution can be just only Free value will be written in new txt file - f.e 3 and 9 . so the last string in line which contains specific words

如果不可能 - 其他解决方案只能是 Free 值将写入新的 txt 文件 - fe 3 和 9 。所以包含特定单词的行中的最后一个字符串

thank you for any tips

谢谢你的任何提示

回答by paxdiablo

Not sure where you got the figure of 9for the Intra_CLIENT_License(souldn't it be 4?), but this script will print out the free licences for those two product codes:

不知道你在哪里得到的这个数字9Intra_CLIENT_License(souldn't说,它是4?),但这个脚本会打印出了这两个产品代码的免费电视牌照:

@echo off
setlocal enableextensions enabledelayedexpansion
for /f "tokens=1,3" %%a in ('type infile.txt') do (
    if "x%%a"=="xSingleSite_License" (
        echo %%a %%b
    )
    if "x%%a"=="xIntra_CLIENT_License" (
        echo %%a %%b
    )
)
endlocal

I basically cut and pasted your transcript into the infile.txtfile and ran this script to get:

我基本上将您的成绩单剪切并粘贴到infile.txt文件中并运行此脚本以获取:

SingleSite_License 3
Intra_CLIENT_License 4

(you can replace type infile.txtwith whatever command you need to generate that output).

(您可以替换type infile.txt为生成该输出所需的任何命令)。



Breakdown:

分解:

The setlocaljust sets up cmd.exeto allow extensions and delayed expansion of environment variables. All my scripts start with this since it's so useful.

setlocal刚刚设置了cmd.exe允许扩展和环境变量的延迟扩展。我所有的脚本都以此开头,因为它非常有用。

The forloop basically processes one line at a time, grabbing tokens 1 and 3 (aand b) from each.

for循环基本上一次处理一行,从每个中获取标记 1 和 3(ab)。

Then it's a simple matter of checking afor the required licence values and, if it matches, outputting that along with b.

然后检查a所需的许可证值是一个简单的问题,如果匹配,则将其与b.

回答by Delan Azabani

Write a C application. Batch is unnecessarily kludgy and probably can't even do this kind of stream manipulation.

编写一个 C 应用程序。Batch 是不必要的混乱,甚至可能无法进行这种流操作。

回答by Jens Bj?rnhager

Download UnxUtilsand use the Unix stream processors. Might be possible to do with Powershell too.

下载UnxUtils并使用 Unix 流处理器。也可以使用 Powershell。