Loading...

Get Specific Text in Bash Output

Answer #1 100 %

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

You’ll also like:


© 2022 CodeForDev.com -