如何使用批处理按字符串过滤txt文件
我知道这种问题对你来说很容易,但我是批处理模式的初学者。
问题是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不确定您从哪里获得
Intra_CLIENT_License
的9
数字(不应该是4
吗?),但此脚本将打印出这两个产品代码的免费许可证:我基本上将您的成绩单剪切并粘贴到
infile.txt
文件中,然后运行此脚本以获取:(您可以替换
type infile.txt
使用生成该输出所需的任何命令)。分解:
setlocal
只是设置cmd.exe
以允许环境变量的扩展和延迟扩展。我所有的脚本都以此开始,因为它非常有用。for
循环基本上一次处理一行,从每一行获取标记 1 和 3(a
和b
)。然后,只需检查
a
中所需的许可证值,如果匹配,则将其与b
一起输出。Not sure where you got the figure of
9
for theIntra_CLIENT_License
(souldn't it be4
?), but this script will print out the free licences for those two product codes:I basically cut and pasted your transcript into the
infile.txt
file and ran this script to get:(you can replace
type infile.txt
with whatever command you need to generate that output).Breakdown:
The
setlocal
just sets upcmd.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
andb
) from each.Then it's a simple matter of checking
a
for the required licence values and, if it matches, outputting that along withb
.编写一个 C 应用程序。批处理是不必要的混乱,甚至可能无法进行这种流操作。
Write a C application. Batch is unnecessarily kludgy and probably can't even do this kind of stream manipulation.
下载 UnxUtils 并使用 Unix 流处理器。或许也可以使用 Powershell 来完成。
Download UnxUtils and use the Unix stream processors. Might be possible to do with Powershell too.