dm03514
dm03514

Reputation: 55972

Extract regex from file using linux utility. print each match on a seperate line

I am trying to extract values from a file using a regular expresion.

I have 2 xml files. Each one is composed of lots of hotspots which has attributes Xand Y, (coordinates), like:

 <HOTSPOT ID="476" NAME="19.328" MEDIATYPE="url" MEDIA="/flash/hiddenHotspot.png" X="1427" Y="1989" WIDTH="280" HEIGHT="280" ZOOM="83" XSCALE="300" YSCALE="300" URL="/archive/manuscripts/1/decode?detail=476&amp;page=0" URLTARGET="_self" ROLLOVER="0" CAPTION="328" TOOLTIP="">
    <TOOLTIP>&lt;b&gt;19.328&lt;/b&gt;.&lt;br&gt;&lt;p class="poem1"&gt;In the Year 10 House, on the Day 11 Monkey,&lt;/p&gt;</TOOLTIP>
  </HOTSPOT>

I am trying to extract just the X value from the file. I started with grep but it returns the full lines. My regex is just /X="([0-9]+)"/. I am on ubuntu 12.04. I'm sure that one of these built in utilities can help but I have not been able to find which one.

Is there a way to just print to stdout the matches of my regex using a linux utility? Each match should be on a seperate line? Could someone please point me in the right direction? Ty.

Upvotes: 0

Views: 521

Answers (1)

Ωmega
Ωmega

Reputation: 43693

Use grep -o with regex pattern (?<=\bX=")([^"]+)(?=")

Upvotes: 1

Related Questions