1

I want to get value inside </em>4,519</a> tag via shell script anyone please help how can do that?

id='idusedMemory' alt='graph'/></em>4,519</a> Mb / 64,309 Mb&nbsp;&nbsp;&nbsp;</td><td>
ROB ENGG
  • 67
  • 1
  • 1
  • 7

3 Answers3

1

Using grep that supports/has the -P flag.

grep -Po '(?<=</em>).*(?=</a>)' file

or

echo 'id='idusedMemory' alt='graph'/></em>4,519</a> Mb / 64,309 Mb&nbsp;&nbsp;&nbsp;</td><td>' | grep -Po '(?<=</em>).*(?=</a>)'

As what was suggested in the comments, don't parse html/xml with such tools. Use a tool/utility for parsing such files.

Jetchisel
  • 7,493
  • 2
  • 19
  • 18
0

Just use grep with the -o switch in order only to show that information:

grep -o "</em>.*</a>" test.txt

.* stands for any number of any character.

Dominique
  • 16,450
  • 15
  • 56
  • 112
0

If your HTML string containing only one substring like that, you can use regexp and sed:

echo "id='idusedMemory' alt='graph'/></em>4,519</a> Mb / 64,309 Mb&nbsp;&nbsp;&nbsp;</td><td>" | sed -rn 's@^.*</em>(.*)</a>.*$@\1@p'

Output:

4,519

If you have something more complicated, you may want to check parsing XML in bash. E.g., here.

Hope that helps.

Emil Viesná
  • 88
  • 1
  • 12