Reputation: 23
I want to get the ip only from this output but idk how.Is it possible to limit the output to only what i want.
VPN Connection Setting Name |kap1
Destination VPN Server Host Name |159.89.199.192
Destination VPN Server Port Number |5252
Destination VPN Server Virtual Hub Name |kap
Proxy Server Type |Direct TCP/IP
Another problem. The ip have port. I only want the ip. Sorry for another question. I appreciate your help you all.
VPN Connection Setting Name |kap1
Status |Connected
VPN Server Hostname |159.89.199.192:5252 (Direct TCP/IP Connection)
Upvotes: 2
Views: 908
Reputation: 58558
This might work for you (GNU sed):
sed -r '/\n/!s/([0-9]{1,3}\.){3}[0-9]{1,3}/\n&\n/;/^\S+[0-9]\n/P;D' file
Surround all ip addresses by newlines and only print those strings that match.
The first regexp looks for newlines. Newlines are what sed uses to delimit strings that it places in the pattern space (PS). Therefore, normally, newlines will never be found naturally in the PS unless they are introduced by the user by way of a substitution command. So the first command says "Unless there are already newlines in the pattern space i.e. the following substitution has already been made. Surround a string that looks like an IP address with newlines". An IP address is matched by one to three digits followed by a period, occurring three times followed by one to three digits. The next sed command, prints any such IP addresses and the last command removes the first line in the PS regardless of whether it was an IP address. If the PS is empty, following the D
command, the next line is read in otherwise sed cycles through the commands again. So the overall effect of the commands is to list out all IP addresses on separate lines.
Upvotes: 2
Reputation: 133750
EDIT: Since OP is saying OP have a different kind of Input_file so adding solution accordingly now.
awk '/Hostname/ && match($0,/[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/){print substr($0,RSTART,RLENGTH)}' Input_file
Following awk
may help you here.
awk -F"|" '/Destination VPN Server Host Name/ && $2 ~ /[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/{print $2}' Input_file
OR
your_command | awk -F"|" '/Destination VPN Server Host Name/ && $2 ~ /[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/{print $2}'
Upvotes: 3
Reputation: 12456
Imagine you have an input file:
$ cat vpn_info
Item |Value
--------------------------------------+--------
VPN Connection |kap1
Destination VPN Server Host Name |159.89.199.192
Destination VPN Server Port Number |5252
...
...
Then you can use one of the following commands:
AWK:
awk -F '|' '/^Destination VPN Server Host Name/{print $2}' vpn_info
159.89.199.192
You define |
as a field separator then you print the 2nd field of your input when the line starts with Destination VPN Server Host Name
SED:
sed -n 's/^Destination VPN Server Host Name *|//p' vpn_info
159.89.199.192
You ask sed
to replace ^Destination VPN Server Host Name *|
by nothing and when it works you print the result.
2nd input:
$ cat vpn_info
Item |Value
--------------------------------------+--------
VPN Connection |kap1
VPN Server Hostname |159.89.199.192:5252 (Direct TCP/IP Connection)
AWK
$ awk -F '[:|]' '/VPN Server Hostname/{print $2}' vpn_info
159.89.199.192
You can use this awk
command where you specify 2 field separators :
and |
and you print the 2nd field when you reach the line that contains VPN Server Hostname
Upvotes: 2
Reputation: 626
Considering the data you have shared is saved in a file. File name : file.txt
$grep 'VPN Server Host Name' file.txt | awk -F '|' '{print $2}'
159.89.199.192
Upvotes: 3