如何使用 sed 从输出字符串中提取 Nginx 版本号(仅)? [英] How to use sed to extract Nginx version number (only) from output string?

查看:141
本文介绍了如何使用 sed 从输出字符串中提取 Nginx 版本号(仅)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个奇怪的情况:我试图用 sed 提取 nginx 的版本,但它不起作用,只有字符串版本有效:

I'm stuck with a weird situation: I'm trying to extract the version of nginx with sed, but it does not working, only the string version works:

[root@hostname~]# echo $(nginx -v) | sed -n 's/nginx version: nginx\///p'
nginx version: nginx/1.16.1

[root@hostname~]# nginx -v | sed -n 's/nginx version: nginx\///p'
nginx version: nginx/1.16.1

[root@hostname~]# echo 'nginx version: nginx/1.16.1' | sed -n 's/nginx version: nginx\///p'
1.16.1

有什么线索吗?

推荐答案

With :

nginx -v |& sed 's/nginx version: nginx\///'

甚至更短/更简单:

nginx -v |& grep -oP '/\K.*'

或者对于 POSIX shell:

Or for POSIX shell:

nginx -v 2>&1 | sed 's/nginx version: nginx\///'

|& 部分是一个经典的管道,但使用特殊的STDERR重定向到STDOUT>& 技巧.

The |& part is a classic pipe but redirecting STDERR to STDOUT with the special & trick.

因为nginx -v输出在错误输出上,也就是STDERR.

Because nginx -v output on the error output, aka STDERR.

这篇关于如何使用 sed 从输出字符串中提取 Nginx 版本号(仅)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆