简体   繁体   English

如何从 Shell 脚本打印 IP 地址?

[英]How to print IP address from Shell Script?

Below is my command line下面是我的命令行

$ps -eaf | grep consul_exporter | grep -v grep
root      4020     1  6 Mar20 ?        4-08:51:18 ./consul_exporter --consul.server=10.1.2.133:8500 --kv.prefix=/ --web.listen-address=0.0.0.0:80

I want to get output of IP address(10.1.2.133) alone as output.我想将 IP 地址(10.1.2.133)的 output 单独作为 output。 How to write command line for that?如何为此编写命令行?

If you don't mind a solution which is not elegant, but does the job, just pipe the output of your grep command into如果您不介意一个不优雅但可以完成工作的解决方案,只需 pipe 将grep命令的 output 放入

... | cut -f 2 -d = | cut -f 1 -d :

This splits the line first by the = and then : .这首先用=分割行,然后是: If the pattern is not so regular and the IP address can appear anywhere in the line, pipe it into如果模式不是那么规则并且 IP 地址可以出现在该行中的任何位置,则 pipe 它进入

... | grep -oE '([0-9]{1,3}[.]){3}[0-9]{1,3}'

The -o option just extract the matching pattern(s) from the input. -o选项仅从输入中提取匹配模式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM