Kalin Borisov
Kalin Borisov

Reputation: 1120

Catch specific variable and print his value

Want to catch a specific variable, in this case DataIp, and to print it's value.

Data:

<UnitInstance DataIp="10.1.1.1" Hostname="crg11" ID="30">

Output:

10.1.1.1

Upvotes: 1

Views: 99

Answers (1)

fedorqui
fedorqui

Reputation: 290145

For example....

$ grep -Po '(?<=DataIp=\")[^\"]+' file
10.1.1.1

We look for whatever coming after DataIp=" and print everything till we find a ". Note that the double quotes need to be escaped: \".

With sed:

$ sed 's/.*DataIp=\"\([^\"]*\)\".*/\1/g' file
10.1.1.1

We catch the word after DataIp and then print it.


Considering the general situation in which we want to catch the first word wrapped by ", we can also do it...

With awk:

$ awk -F\" '{print $2}' file
10.1.1.1

We split the records in fields based on " delimiter and print the 2nd one.

With cut:

$ cut -d'"' -f2 file
10.1.1.1

We split the records in fields based on " delimiter and print the 2nd one.

Upvotes: 4

Related Questions