Reputation: 110312
I have a sitemap that I'm trying to search through. The issue is if I do a grep, it returns the entire file, since there are no spaces in it:
rl><loc>http://www.hulu.com/watch/5564</loc><changefreq>daily</changefreq></url><url><loc>http://www.hulu.com/watch/5560</loc><changefreq>daily</changefreq></url><url><loc>http://www.hulu.com/watch/5559</loc><changefreq>daily</changefreq></url><url><loc>http://www.hulu.com/watch/5548</loc><changefreq>daily</changefreq></url><url><loc>http://www.hulu.com/watch/5547</loc><changefreq>daily</changefreq></url><url><loc>http://www.hulu.com/watch/5544</loc><changefreq>daily</changefreq></url><url><loc>http://www.hulu.com/watch/5532</loc><changefreq>daily</changefreq></url><url><loc>http://www.hulu.com/watch/5531</loc><changefreq>daily</changefreq></url><url><loc>http://www.hulu.com/watch/5530</loc><changefreq>daily</changefreq></url><url><loc>http://www.hulu.com/watch/5529</loc><changefreq>daily</changefreq></url><url><loc>http://www.hulu.com/watch/5528</loc><changefreq>daily</changefreq></url><url><loc>http://www.hulu.com/watch/5527</loc><changefreq>daily</changefreq></url><url><loc>http://www.hulu.com/watch/5526</loc><changefreq>daily</changefreq></url><url><loc>http://www.hulu.com/watch/5525</loc><changefreq>daily</changefreq></url><url><loc>http://www.hulu.com/watch/5417</loc><changefreq>daily</changefreq></url><url><loc>http://www.hulu.com/watch/5416</loc><changefreq>daily</changefreq></url><url><loc>http://www.hulu.com/watch/5415</loc><changefreq>daily</changefreq></url><url><loc>http://www.hulu.com/watch/5414</loc><changefreq>daily</changefreq></url><url><loc>http://www.hulu.com/watch/5263</loc><changefreq>daily</changefreq></url><url><loc>http://www.hulu.com/watch/5262</loc><changefreq>daily</changefreq></url><url><loc>http://www.hulu.com/watch/5261</loc><changefreq>daily</changefreq></url><url><loc>http://www.hulu.com/watch/5136</loc><changefreq>daily</changefreq></url><url><loc>http://www.hulu.com/watch/5135</loc><changefreq>daily</changefreq></url><url><l
Is there a way to do grep
such that it only extract a certain number of characters before/after the search match?
I'm currently doing:
$ grep -r '/23407' ./
Upvotes: 0
Views: 35
Reputation: 31
The option -o prints only the matched pattern. Try grep -o <pattern> <file>
Upvotes: 0
Reputation: 52211
With the -o
option, you get only the match itself; to get some characters before and after your match, you can just match any characters – for example for 10 characters before and after, for your example string, with a match that exists:
$ grep -o '.\{10\}/5135.\{10\}' <<< "$string"
.com/watch/5135</loc><cha
Upvotes: 1