linux - 使用 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 动态获取子字符串?我需要使用什么模式?
解决方案
将$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
是包含问题文本的文件,并且打印了正确的行。
推荐阅读
- html - 您如何使用本机中的 ion-menu 为 ionic 中的侧边菜单制作圆角
- google-cloud-platform - 如何在不运行 VM 的情况下在谷歌云中移动实例区域
- html - 无法删除右侧边距
- ruby-on-rails - 在循环内渲染部分
- c++ - c++ 通用空间过滤漏洞利用预防
- sql - 一次运行 1000 个查询的大量更新查询的最佳方法
- javascript - javascript:在链中,如何获取先前数组的大小?
- jquery - 如何在 CSS 修改的单选按钮上设置所需的属性
- php - DOMPDF 更改 html 元素的位置
- c# - 来自具有相同父级的两个用户控件的绑定属性