Windows cmd - findstr 和 gawk 或者如何提取以下数据

发布于 2024-12-28 13:52:32 字数 713 浏览 7 评论 0原文

我想知道是否可以使用 findstr 和/或 gawk 来完全按照我需要的方式返回 Windows cmd 的输出。我目前正在返回原始输出,然后删除空白行,并解析出我需要的内容。作为一种学习,我希望我能看到如何做得更好。

原始输出:

Change 2086888 on 2012/01/23 by user1@server1

        test_description_1.2.3.4@29816

Change 2086888 on 2012/01/23 by user1@server2

        test_description2_4.5.6.7@29816

Change 2078677 on 2012/01/20 by user2@server1

        test_description3_7.8.9.10@29816

我获取该输出并使用 php 将其解析为:

1. 2086888,test_description_1.2.3.4@29816
2. 2086888,test_description2_4.5.6.7@29816
3. 2078677,test_description3_7.8.9.10@29816

为了让自己更容易一些,我通过将其管道传输到 findstr 并使用 /V "^$" 来从输出中删除空行。所以是|查找str /V“^$”。

如何直接从命令行获取用 php 解析的输出?

I was wondering if it's possible to use findstr and/or gawk to return the output of a windows cmd exactly how I need it to be. I'm currently returning the output raw, then stripping out the blank lines, and parsing out what I need. Just as a learning thing, I was hoping I could see how this can be done better.

The raw output:

Change 2086888 on 2012/01/23 by user1@server1

        test_description_1.2.3.4@29816

Change 2086888 on 2012/01/23 by user1@server2

        test_description2_4.5.6.7@29816

Change 2078677 on 2012/01/20 by user2@server1

        test_description3_7.8.9.10@29816

I take that output and parse it out to this with php:

1. 2086888,test_description_1.2.3.4@29816
2. 2086888,test_description2_4.5.6.7@29816
3. 2078677,test_description3_7.8.9.10@29816

To make it a little easier on myself, I remove the blank lines from the output by piping it to findstr and using /V "^$". So it's | findstr /V "^$".

How can I get the output that I parse out with php directly from the command line?

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

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

发布评论

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

评论(1

韵柒 2025-01-04 13:52:32

cmd.exe 中的 for 命令会跳过空行,因此这样解析可能会更容易:

@Echo Off
setlocal enabledelayedexpansion
set linenum=1
for /f "tokens=1,2" %%A in (output.txt) do @(
  if %%A==Change set result=%%B
  if not %%A==Change (
    echo !linenum!. !result!,%%A
    set /a linenum=!linenum!+1
  )
)   
endlocal

for command in cmd.exe skips blank lines, so it may be easier to parse it this way:

@Echo Off
setlocal enabledelayedexpansion
set linenum=1
for /f "tokens=1,2" %%A in (output.txt) do @(
  if %%A==Change set result=%%B
  if not %%A==Change (
    echo !linenum!. !result!,%%A
    set /a linenum=!linenum!+1
  )
)   
endlocal
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文