There's no need to make it that complicated. Just use the "-n
" option to silence output by default, then add the "p
" modifier directly to the substitute command. That way only lines that match will be printed.
You can also use the "-r
" option to explicitly enable extended regex, so that there's no need to escape the parentheses.
Since the ".
" represents any character, we really don't want to use it when we actually want to match "/
". Unfortunately, this is the default delimiter for the "s
" command. However, sed
allows you to use any ascii character as the delimiter, so just choose one that's not found in the expression itself. I prefer using "|
Next, the regex only really needs to contain enough of the string to ensure a unique match, and the starting and ending anchors are also superfluous here, as the regex assumes them. Of course, it doesn't hurt to leave them in either.
Finally, it's probably safer to replace the ".*
" (a string of characters of any length) with "[^<]*
" (a string of "not <" of any length), to avoid any possible issues with regex greediness.
sed -rn '/status/ s|.*status>([^<]*)</ns0:stat.*|\1|p' logfile
BTW, I don't see where the line-ending issue could be coming from. sed always appends a newline to each line of output anyway.
Here are a few useful sed references.