我有一个名为LogsDocker的文件
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
192.168.99.100刚刚发现了Awk并使用命令
awk 'BEGIN { FS = "//"} ; { print $2}' LogsDocker把它打印出来(用一堆空行)
192.168.99.100:2376"怎样才能正确地打印出没有空行的ip?
发布于 2019-12-04 15:25:01
你能试一下吗。
awk '/DOCKER_HOST/ && match($0,/[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/){print substr($0,RSTART,RLENGTH)}' Input_file解释:添加对上述代码的解释。
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总是相同的,然后尝试如下。
awk -F'[/:]' '/DOCKER_HOST/{print $4}' Input_file解释:添加对上述代码的解释。
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溶液。
sed -n '/DOCKER_HOST/s/.*\///;s/:.*//p' Input_fileExplanation:以下仅供解释之用。
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.发布于 2019-12-04 15:25:50
假设给定现有代码,IP地址是您拥有//的唯一位置
$ awk 'sub(/.*\/\//,""){sub(/:.*/,""); print}' file
192.168.99.100或作其他假设.
$ awk -F'//|:' 'NF>2{print $3}' file
192.168.99.100或者:
$ awk -F'//|:' '/DOCKER_HOST=/{print $3}' file
192.168.99.100或者..。
这实际上取决于该文件中的其他内容,以及您希望它有多健壮。
https://stackoverflow.com/questions/59179283
复制相似问题