首页 > 解决方案 > 使用 Bash Perl 从命令输出中获取子字符串

问题描述

假设我正在使用的文本是(由 输出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 动态获取子字符串?我需要使用什么模式?

标签: linuxbashperlsubstring

解决方案


$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=.*)"/'

whereoutput.txt是包含问题文本的文件,并且打印了正确的行。


推荐阅读