Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用regex和awk来检测和提取可变长度和宽度的文本表?_Awk - Fatal编程技术网

如何使用regex和awk来检测和提取可变长度和宽度的文本表?

如何使用regex和awk来检测和提取可变长度和宽度的文本表?,awk,Awk,在运行一些更新WordPress的脚本时,脚本的输出被记录到一个文件中。以下是日志文件的相关部分: Downloading update from https://downloads.wordpress.org/plugin/adrotate.5.8.15.zip... Unpacking the update... Installing the latest version... Removing the old version of the plugin... Plugin updated

在运行一些更新WordPress的脚本时,脚本的输出被记录到一个文件中。以下是日志文件的相关部分:

Downloading update from https://downloads.wordpress.org/plugin/adrotate.5.8.15.zip...
Unpacking the update...
Installing the latest version...
Removing the old version of the plugin...
Plugin updated successfully.
Downloading update from https://downloads.wordpress.org/plugin/cookie-notice.2.0.0.zip...
Unpacking the update...
Installing the latest version...
Removing the old version of the plugin...
Plugin updated successfully.
Downloading update from https://downloads.wordpress.org/plugin/google-site-kit.1.25.0.zip...
Unpacking the update...
Installing the latest version...
Removing the old version of the plugin...
Plugin updated successfully.
Disabling Maintenance mode...
+-----------------+-------------+-------------+---------+
| name            | old_version | new_version | status  |
+-----------------+-------------+-------------+---------+
| adrotate        | 5.8.14      | 5.8.15      | Updated |
| cookie-notice   | 1.3.2       | 2.0.0       | Updated |
| google-site-kit | 1.24.0      | 1.25.0      | Updated |
+-----------------+-------------+-------------+---------+
[32;1mSuccess:[0m Updated 3 of 3 plugins.
[32;1mSuccess:[0m Theme already updated.
接下来我要做的是打开、读取并提取日志文件的一部分,将其按原样写入一个单独的文件。我需要的关键部分是上面输出的表格:

+-----------------+-------------+-------------+---------+
| name            | old_version | new_version | status  |
+-----------------+-------------+-------------+---------+
| adrotate        | 5.8.14      | 5.8.15      | Updated |
| cookie-notice   | 1.3.2       | 2.0.0       | Updated |
| google-site-kit | 1.24.0      | 1.25.0      | Updated |
+-----------------+-------------+-------------+---------+
所以,我现在做的是使用

awk'/禁用维护模式…$/,/[32;1mSuccess:$/”logfile.txt

不幸的是,通过这个awk命令,我似乎也获得了禁用维护模式…和[32;1mSuccess:部分与它一起。而且这些字符串不够可靠一致,无法将它们用作awk的正确开始/结束标记。我能想到的最准确的方法是使用正确的正则表达式来获取该表,而不是更多

文本格式表的问题在于,它的长度和宽度可能会根据脚本更新的内容而有所不同。例如,名称列中可能有一个50个字符长的项,这使表更宽。它也可能有20行。因此,我不知道在正则表达式或在某种循环中

我尝试过各种教程,也尝试过regex101.com来设计一种模式来帮助我找到这个可变长度/宽度的模式。但我没有取得任何进展。我不确定我是否知道如何在regex语法中正确地构建问题。我正在阅读的所有教程都使用abc和xxx作为示例,这要复杂得多


有谁能帮我弄清楚如何做到这一点吗?

对于您展示的示例,请尝试以下内容。用GNU awk编写并测试

说明:增加对以上内容的详细说明

awk '                                       ##Starting awk program from here.
/^\[32;1mSuccess:/      { found=""      }   ##Checking if line starts from [32;1mSuccess: then unset found here.
/^Disabling Maintenance/{ found=1; next }   ##Checking if line starts from Disabling Maintenance then set found to 1 here.
found                                       ##checking condition if found is set(NOT NULL) then print that line.
' Input_file                                ##Mentioning Input_file name here.

有了你们展示的样品,请尝试以下。用GNU awk编写和测试

说明:增加对以上内容的详细说明

awk '                                       ##Starting awk program from here.
/^\[32;1mSuccess:/      { found=""      }   ##Checking if line starts from [32;1mSuccess: then unset found here.
/^Disabling Maintenance/{ found=1; next }   ##Checking if line starts from Disabling Maintenance then set found to 1 here.
found                                       ##checking condition if found is set(NOT NULL) then print that line.
' Input_file                                ##Mentioning Input_file name here.

也许这太简单了

awk 'bar == 3 {exit}; /--/ {bar++} bar ' logfile.txt
如果您不想在输出中使用条形图:

awk 'bar == 3 {exit}; /--/ {bar++; next} bar' logfile.txt

也许这太简单了

awk 'bar == 3 {exit}; /--/ {bar++} bar ' logfile.txt
如果您不想在输出中使用条形图:

awk 'bar == 3 {exit}; /--/ {bar++; next} bar' logfile.txt
我会按照下面的方式使用GNU AWK,让file.txt内容

Downloading update from https://downloads.wordpress.org/plugin/adrotate.5.8.15.zip...
Unpacking the update...
Installing the latest version...
Removing the old version of the plugin...
Plugin updated successfully.
Downloading update from https://downloads.wordpress.org/plugin/cookie-notice.2.0.0.zip...
Unpacking the update...
Installing the latest version...
Removing the old version of the plugin...
Plugin updated successfully.
Downloading update from https://downloads.wordpress.org/plugin/google-site-kit.1.25.0.zip...
Unpacking the update...
Installing the latest version...
Removing the old version of the plugin...
Plugin updated successfully.
Disabling Maintenance mode...
+-----------------+-------------+-------------+---------+
| name            | old_version | new_version | status  |
+-----------------+-------------+-------------+---------+
| adrotate        | 5.8.14      | 5.8.15      | Updated |
| cookie-notice   | 1.3.2       | 2.0.0       | Updated |
| google-site-kit | 1.24.0      | 1.25.0      | Updated |
+-----------------+-------------+-------------+---------+
[32;1mSuccess:[0m Updated 3 of 3 plugins.
[32;1mSuccess:[0m Theme already updated.
然后

输出

+-----------------+-------------+-------------+---------+
| name            | old_version | new_version | status  |
+-----------------+-------------+-------------+---------+
| adrotate        | 5.8.14      | 5.8.15      | Updated |
| cookie-notice   | 1.3.2       | 2.0.0       | Updated |
| google-site-kit | 1.24.0      | 1.25.0      | Updated |
+-----------------+-------------+-------------+---------+
说明:只打印以+|之一开头,以+|之一结尾的行。请注意,如果有任何非表行以+或|开头,以+或|结尾,则可能会出现误报。因此,如果希望使用我的解决方案,我建议您使用输入数据运行进一步的测试。

我将按照以下方式使用GNU AWK,let file.txt con帐篷

Downloading update from https://downloads.wordpress.org/plugin/adrotate.5.8.15.zip...
Unpacking the update...
Installing the latest version...
Removing the old version of the plugin...
Plugin updated successfully.
Downloading update from https://downloads.wordpress.org/plugin/cookie-notice.2.0.0.zip...
Unpacking the update...
Installing the latest version...
Removing the old version of the plugin...
Plugin updated successfully.
Downloading update from https://downloads.wordpress.org/plugin/google-site-kit.1.25.0.zip...
Unpacking the update...
Installing the latest version...
Removing the old version of the plugin...
Plugin updated successfully.
Disabling Maintenance mode...
+-----------------+-------------+-------------+---------+
| name            | old_version | new_version | status  |
+-----------------+-------------+-------------+---------+
| adrotate        | 5.8.14      | 5.8.15      | Updated |
| cookie-notice   | 1.3.2       | 2.0.0       | Updated |
| google-site-kit | 1.24.0      | 1.25.0      | Updated |
+-----------------+-------------+-------------+---------+
[32;1mSuccess:[0m Updated 3 of 3 plugins.
[32;1mSuccess:[0m Theme already updated.
然后

输出

+-----------------+-------------+-------------+---------+
| name            | old_version | new_version | status  |
+-----------------+-------------+-------------+---------+
| adrotate        | 5.8.14      | 5.8.15      | Updated |
| cookie-notice   | 1.3.2       | 2.0.0       | Updated |
| google-site-kit | 1.24.0      | 1.25.0      | Updated |
+-----------------+-------------+-------------+---------+

说明:仅打印以+|之一开头,以+|之一结尾的行。请注意,如果有任何非表格行以+或|开头,以+或|结尾,则可能会出现误报。因此,如果您希望使用我的解决方案,我建议您使用输入数据运行进一步的测试。

您是否可以添加更清晰的样本而不使用我的解决方案+--------输入和预期输出中的行。也请在您的问题中以代码的形式添加您的努力,谢谢。我不确定您在问@RavinderSingh13什么。您能澄清一下吗?首先您的示例不清楚,所以请删除+-----------行,如果它们在您的实际文件中不存在,如果存在,请确认它们确实存在。然后在您的问题中更清楚地添加您的预期输出。最后,重要的一点是在您的问题中添加您的努力,谢谢。这是否回答了您的问题?我已更新了问题,以澄清我尝试了什么以及我正在尝试做什么。@AkshayHegde,感谢您提供的链接,但这是为了说明我是否希望从其他数据生成一个表。日志文件已经有格式化的表格在里面。我只想用awk/regex抓取它并将它输出到另一个文件中。你能添加更多清晰的样本,输入和预期输出中没有+----------行吗。另外,请在你的问题中以代码的形式添加你的努力,谢谢。我不确定你在问@RavinderSingh13什么。你能澄清一下吗?First您的示例不清楚,因此请删除+-----------行,如果它们在实际文件中不存在,如果存在,则确认它们确实存在。然后在问题中更清楚地添加您的预期输出。最后,重要的一点是在您的问题中添加您的努力,谢谢。这是否回答了您的问题?我已更新了问题n以澄清我尝试了什么以及我正在尝试做什么。@AkshayHegde,谢谢你的链接,但这是为了我是否想从其他数据生成一个表。日志文件中已经有格式化的表。我只想用awk/regex获取它并将其输出到另一个文件。谢谢。根据你刚才的解释,这看起来很棒,很有意义然而,奇怪的是,当我运行awk命令时,什么都没有发生。相比之下,对于下面Misha的回答,他的例子是有效的。我想知道这是否是语法方面的问题?谢谢。根据你给出的解释,这看起来很好,很有意义。然而,奇怪的是,当我运行awk命令时
什么也没发生。相比之下,对于下面米沙的回答,他的例子是有效的。我想知道这是否是语法问题?@user3169905,Hi,IMHO您现在接受的答案将失败,如果您有--比示例中显示的多行,此解决方案将更仔细地检查条件,谢谢。@user3169905,Hi,IMHO您现在接受的答案将失败,如果您有--比示例中显示的多行,此解决方案更仔细地检查条件,谢谢。