-1

I have some lines in an mpd file as mentioned below. I want to grep only the words ending with .ts and save them in a different file. Can someone help me on that?

    <SegmentURL media="1.ts" mediaRange="0-3424419"/>
 <SegmentURL media="2.ts" mediaRange="0-8063319"/>
 <SegmentURL media="3.ts" mediaRange="0-7146631"/>
 <SegmentURL media="4.ts" mediaRange="0-8984143"/>
Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
Raj
  • 71
  • 1
  • 6

2 Answers2

0
$ grep -o '\<\w*\.ts\>' infile
1.ts
2.ts
3.ts
4.ts
  • \< and \> are beginning/end of word markers
  • \w is a synonym for [_[:alnum:]] (just [[:alnum:]] in BSD grep)
  • -o retains only the matches

To save the output to a file output, redirect with > output appended to the command.

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
0

For given example, you don't need the PCRE, BRE is sufficient for your needs.

grep -o '[^"][.]ts' file

But keep in mind that grep does line-based match. If your input file is html/xml, particularly, if it was generated by some application, the lines could be broken. Thus, grep may fail.

Using a xml/html parser if that was the case.

Kent
  • 189,393
  • 32
  • 233
  • 301