echo 'Subject : "O=This is a test,CN=abc.def.com,L=North,ST=Arizona,C=CA"' |sed -r 's/.*CN=([^,]*)[,].*/\1/'
abc.def.com
linux bash sed: '-r' allows to use '()' without escaping search pattern: any char and "CN=" : .*CN= ( any char but not ',': [^,]* ) # remember/save this part in var 1 ',' and any char: [,].* replace with saved buffer/variable: \1
Cutting out strings is a very common task and can be done in various ways as shown above already: so I am also always looking for new ways and always taking into account the available tools and patterns to have an easy, fast and precise solution.
Happy cutting!
brgds
stefan