【问题标题】:Use Bash Perl to fetch substring off of command output使用 Bash Perl 从命令输出中获取子字符串
【发布时间】:2020-05-19 12:27:56
【问题描述】:

假设我正在使用的文本是(由pecl install xdebug 输出):

  |   - A list of all settings:  https://xdebug.org/docs-settings.php    |
  |   - A list of all functions: https://xdebug.org/docs-functions.php   |
  |   - Profiling instructions:  https://xdebug.org/docs-profiling2.php  |
  |   - Remote debugging:        https://xdebug.org/docs-debugger.php    |
  |                                                                      |
  |                                                                      |
  |   NOTE: Please disregard the message                                 |
  |       You should add "extension=xdebug.so" to php.ini                |
  |   that is emitted by the PECL installer. This does not work for      |
  |   Xdebug.                                                            |
  |                                                                      |
  +----------------------------------------------------------------------+


running: find "/tmp/pear/temp/pear-build-defaultuserNxuIJy/install-xdebug-2.9.2" | xargs ls -dils
1078151    4 drwxr-xr-x 3 root root    4096 Feb  3 17:40 /tmp/pear/temp/pear-build-defaultuserNxuIJy/install-xdebug-2.9.2
1078337    4 drwxr-xr-x 3 root root    4096 Feb  3 17:40 /tmp/pear/temp/pear-build-defaultuserNxuIJy/install-xdebug-2.9.2/usr
1078338    4 drwxr-xr-x 3 root root    4096 Feb  3 17:40 /tmp/pear/temp/pear-build-defaultuserNxuIJy/install-xdebug-2.9.2/usr/local
1078339    4 drwxr-xr-x 3 root root    4096 Feb  3 17:40 /tmp/pear/temp/pear-build-defaultuserNxuIJy/install-xdebug-2.9.2/usr/local/lib
1078340    4 drwxr-xr-x 3 root root    4096 Feb  3 17:40 /tmp/pear/temp/pear-build-defaultuserNxuIJy/install-xdebug-2.9.2/usr/local/lib/php
1078341    4 drwxr-xr-x 3 root root    4096 Feb  3 17:40 /tmp/pear/temp/pear-build-defaultuserNxuIJy/install-xdebug-2.9.2/usr/local/lib/php/extensions
1078342    4 drwxr-xr-x 2 root root    4096 Feb  3 17:40 /tmp/pear/temp/pear-build-defaultuserNxuIJy/install-xdebug-2.9.2/usr/local/lib/php/extensions/no-debug-non-zts-20180731
1078336 2036 -rwxr-xr-x 1 root root 2084800 Feb  3 17:40 /tmp/pear/temp/pear-build-defaultuserNxuIJy/install-xdebug-2.9.2/usr/local/lib/php/extensions/no-debug-non-zts-20180731/xdebug.so

Build process completed successfully
Installing '/usr/local/lib/php/extensions/no-debug-non-zts-20180731/xdebug.so'
install ok: channel://pecl.php.net/xdebug-2.9.2
configuration option "php_ini" is not set to php.ini location
You should add "zend_extension=/usr/local/lib/php/extensions/no-debug-non-zts-20180731/xdebug.so" to php.ini

我想从这个输出中提取这部分并将其保存在一个变量中以供以后使用:

zend_extension=/usr/local/lib/php/extensions/no-debug-non-zts-20180731/xdebug.so

我曾尝试用 Perl 这样做,但没有成功:

echo $OUTPUT | perl -lne 'm/You should add "(.*)"/; print $1'

如何使用 perl 动态获取子字符串?我需要使用什么模式?

【问题讨论】:

  • variable=$(echo $OUTPUT | grep "enter text here" | cut -d'"' -f2 也许?
  • 我需要模式方面的帮助,因为我无法从输出中获取路径。将其包含到变量中的实际逻辑不是问题
  • 你为每一行打印$1,最初是未定义的,然后在第8行之后打印"extension=xdebug.so",直到29,这将是你需要的。匹配成功时才需要打印,需要细化匹配只匹配zend_extension
  • 不清楚 OP 对以 You should add 开头的行或任何以 zend_extension= 开头的行感兴趣

标签: linux bash perl substring


【解决方案1】:

$OUTPUT 文本放在output.txt 文件中

cat output.txt | perl -wnE'say $1 if /You should add "(zend_extension=.*)"/'

这使用了显示文本的细节,特别是看似独特的路径前言zend_extension=...,以区分所需的行和早期的“您应该添加”模式。根据需要更改为更适合您的问题的内容。

如果文本在代码中作为一个字符串在单行中抛出,则添加 -0777 标志进行测试。

否则请说明$OUTPUT 是如何产生的。


使用 bash 脚本测试

#!/bin/bash
# Last modified: 2020 Feb 03 (12:58)

OUTPUT=$(cat "output.txt")

echo $OUTPUT | perl -wnE'say $1 if /You should add "(zend_extension=.*)"/'

其中output.txt 是包含问题文本的文件,并且打印了右侧行。

【讨论】:

  • 抱歉不清楚。 $OUTPUT 基本上是指:OUTPUT=$(pecl install xdebug)。这有意义吗?
  • 它适用于cat,但不适用于echo,例如:echo $OUTPUT | perl -wnE 'say $1 if /You should add "(zend_extension=.*)"/'。我正在使用 Debian 10
  • @Dugi 所以我现在使用了一个基本的 bash 脚本,我在其中执行 OUTPUT=$(cat "output.txt"),其中 "output.txt" 是我在其中复制的文件-从问题中粘贴您的文字。然后我将这个答案中的单行复制粘贴到echo $OUTPUT | perl... - 它可以工作,它会打印以筛选感兴趣的行。这与您的情况有何不同? (我不能这样做 pecl 的事情,但这不重要)
  • @Dugi 将上述评论中的声明添加到帖子中
【解决方案2】:

你可以使用这个perl

perl -lne 'print $1 if /You should add "(?!extension=xdebug\.so)([^"]+)"/' <<< "$OUTPUT"

zend_extension=/usr/local/lib/php/extensions/no-debug-non-zts-20180731/xdebug.so

负前瞻 (?!extension=xdebug\.so) 将忽略输出中的行 extension=xdebug.so

或者你可以在行首匹配You should add

perl -lne 'print $1 if /^You should add "([^"]+)"/' <<< "$OUTPUT"

【讨论】:

  • 负前瞻在这里是多余的
  • 是的,这就是为什么还有一个替代解决方案
【解决方案3】:

可能是 OP 打算使用

echo $OUTPUT | perl -ne 'm/You should add "(.*)"/ &amp;&amp; print $1'

echo $OUTPUT | perl -ne 'print $1 if m/You should add "(.*)"/'

【讨论】:

    猜你喜欢
    • 2012-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-31
    • 1970-01-01
    • 2020-03-22
    • 2013-08-12
    • 2012-02-23
    相关资源
    最近更新 更多