简体   繁体   中英

Make awk print a specific segment of a single line

I have a file called LogsDocker that contains

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)

and i want to print only the ip

192.168.99.100

Just discovered Awk and with the command

 awk 'BEGIN { FS = "//"} ; { print $2}' LogsDocker

made it print (with a bunch of empty lines)


192.168.99.100:2376"




What would be the correct way to print ONLY the ip without empty lines

Could you please try following.

awk '/DOCKER_HOST/ && match($0,/[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/){print substr($0,RSTART,RLENGTH)}' Input_file

Explanation: Adding explanation for above code.

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.


2nd solution: Considering that your Input_file will always be same then try following.

awk -F'[/:]' '/DOCKER_HOST/{print $4}' Input_file

Explanation: Adding explanation for above code.

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.


3rd solution: A sed solution.

sed -n '/DOCKER_HOST/s/.*\///;s/:.*//p'  Input_file

Explanation: Following is only for explanation purposes.

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.

Assuming given your existing code that the IP address is the only place you have // :

$ awk 'sub(/.*\/\//,""){sub(/:.*/,""); print}' file
192.168.99.100

or making other assumptions...:

$ awk -F'//|:' 'NF>2{print $3}' file
192.168.99.100

or:

$ awk -F'//|:' '/DOCKER_HOST=/{print $3}' file
192.168.99.100

or ....

It really just depends what else is in that file and how robust you want it to be.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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