Grep and print only the PATTERN

pguelpa

Registered
I was wondering if anyone knows how I could search files (I assume using grep) and print out only the text that matches the PATTERN I supply. Or even better, if I could only print out one of the backreferences of the pattern I supply.

The purpose is that I have a bunch of files with text in them that contain [link src="..." type="#text"]...[/link] "tags". And I want to be able to write a script to check that the "..." of the src attribute actually points to a file.

Any thoughts? Thanks in advance for any help.

Cheers,

Paul
 
Do a grep then pipe into sed. sed is pretty eay to use:

grep "pattern" file | sed 's/.*]\(.*\)[.*/\1/'

That is a quick and dirty script.
 
Great, thanks. I got it to work properly, except in the case when there is more than one link on a line. Is there any way to get sed to repeat it's search until it gets to the end of the line? Or maybe it's me regex that's the problem. Here is what I have:

grep -E '\[link src=".*" type="#text"' G.xml | sed 's/.*src="\(.*\)" type="#text".*/\1/'

So what happens if there is more than one "[link src=..." on the line is that sed only seems to get the last one.

Any way around this?

Thanks again for the help,

Paul
 
A couple of things to play with awk, and the very last slash of sed, add a 1 after it.

Just play with the regex.

If the pattern will only be on one line, try using head/tail, after the grep.
 
Back
Top