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

发布于 2024-09-03 00:56:17 字数 1483 浏览 6 评论 0原文

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

问题是 bat 文件代码应该如何从特定的 txt 文件生成新的 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 . . . 

我想在这个新文件中包含的内容只有几行:

SingleSite_License 3

Intra_CLIENT_License 9

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

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

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

谢谢您的任何提示

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 .

for example .

I have report 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

Intra_CLIENT_License 9

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

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

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

thank you for any tips

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

漫漫岁月 2024-09-10 00:56:17

不确定您从哪里获得 Intra_CLIENT_License9 数字(不应该是 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

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

SingleSite_License 3
Intra_CLIENT_License 4

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


分解:

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

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

然后,只需检查 a 中所需的许可证值,如果匹配,则将其与 b 一起输出。

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

@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.txt file and ran this script to get:

SingleSite_License 3
Intra_CLIENT_License 4

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


Breakdown:

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

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

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

我们的影子 2024-09-10 00:56:17

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

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

梦初启 2024-09-10 00:56:17

下载 UnxUtils 并使用 Unix 流处理器。或许也可以使用 Powershell 来完成。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文