首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使awk打印单行的特定段

使awk打印单行的特定段
EN

Stack Overflow用户
提问于 2019-12-04 15:21:22
回答 2查看 201关注 0票数 2

我有一个名为LogsDocker的文件

代码语言:javascript
运行
复制
export DOCKER_TLS_VERIFY="1"
export DOCKER_HOST="tcp://192.168.99.100:2376"
export DOCKER_CERT_PATH="/root/.docker/machine/machines/Main-hola"
export DOCKER_MACHINE_NAME="Main-hola"
# Run this command to configure your shell: 
# eval $(docker-machine env Main-hola)

我只想打印ip

代码语言:javascript
运行
复制
192.168.99.100

刚刚发现了Awk并使用命令

代码语言:javascript
运行
复制
 awk 'BEGIN { FS = "//"} ; { print $2}' LogsDocker

把它打印出来(用一堆空行)

代码语言:javascript
运行
复制
192.168.99.100:2376"

怎样才能正确地打印出没有空行的ip?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-12-04 15:25:01

你能试一下吗。

代码语言:javascript
运行
复制
awk '/DOCKER_HOST/ && match($0,/[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/){print substr($0,RSTART,RLENGTH)}' Input_file

解释:添加对上述代码的解释。

代码语言:javascript
运行
复制
awk '                                                             ##Starting awk program from here.
/DOCKER_HOST/ && match($0,/[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/){      ##Checking condition if string DOCKER_HOST is found in line AND match is having a mentioned regex matched in it.
  print substr($0,RSTART,RLENGTH)                                 ##If above conditions are TRUE then printing substring whose starting index is RSTART and ending index is RLENGTH.
}
' Input_file                                                      ##Mentioning Input_file name here.

第二个解决方案:考虑到您的Input_file总是相同的,然后尝试如下。

代码语言:javascript
运行
复制
awk -F'[/:]' '/DOCKER_HOST/{print $4}' Input_file

解释:添加对上述代码的解释。

代码语言:javascript
运行
复制
awk -F'[/:]' '      ##Starting awk program from here and setting field separator as /or colon here.
/DOCKER_HOST/{      ##Checking condition if a line has string DOCKER_HOST then do following.
  print $4          ##Printing 4th field of current line.
}
' Input_file        ##Mentioning Input_file name here.

第三种解决方案:A sed溶液。

代码语言:javascript
运行
复制
sed -n '/DOCKER_HOST/s/.*\///;s/:.*//p'  Input_file

Explanation:以下仅供解释之用。

代码语言:javascript
运行
复制
sed -n '          ##Starting sed program from here and making printing off for all lines until specifically mentioned.
/DOCKER_HOST/     ##Searching string DOCKER_HOST in lines if present then do following.
s/                ##s means perform substitution operation here.
.*\/              ##mentioning regex which covers everything till / in line, if matched this regex
//                ##Then substitute it with NULL here.
;                 ##semi colon denotes to segregate another substitute operation after this one.
s/                ##Doing substitution from here.
:.*               ##Match everything from : to till last of line.
//                ##Substitute above matched values with NULL in current line.
p                 ##p means only print this line.
'  Input_file     ##Mentioning Input_file name here.
票数 2
EN

Stack Overflow用户

发布于 2019-12-04 15:25:50

假设给定现有代码,IP地址是您拥有//的唯一位置

代码语言:javascript
运行
复制
$ awk 'sub(/.*\/\//,""){sub(/:.*/,""); print}' file
192.168.99.100

或作其他假设.

代码语言:javascript
运行
复制
$ awk -F'//|:' 'NF>2{print $3}' file
192.168.99.100

或者:

代码语言:javascript
运行
复制
$ awk -F'//|:' '/DOCKER_HOST=/{print $3}' file
192.168.99.100

或者..。

这实际上取决于该文件中的其他内容,以及您希望它有多健壮。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59179283

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档