SED! SED! SED! Sed makes you strong! Strength crushes enemies! SED!
-n is the flag which means 'don't output anything unless it's explicitly printed', which you want
s/^.*\(X\).*$/\1/p is the command which means 'print anything which matches regexp X' (although only once per line); remember to escape the backslashes - if you put the whole command in speechmarks, that's enough in bash
-E makes sed use 'modern' regexps, which means you don't need to escape parens to make a group etc; doesn't seem to enable non-greedy quantifiers, though, i think that's a PCRE thing. With this flag, the above command becomes
- If you're sedding paths, remember to escape any / characters in the patterns, or else they'll be interpreted as segment delimiters in the substitution command! Alternatively, use different delimiters - you can use anything. A hash sign (#) is traditional as an alternative, but note tht it won't work quite right unless it's escaped, as the shell will see it as a comment character; putting the whole command in single quotes does the job.