regex - sed command to insert a character before another character with a 'not' match -


file :

#comment1   executable code #comment2  executable code 

i want insert 2 hashes before 2nd , 4th line, tried, sed 's/^[^#]/##/g' replaces first character ('s' in 2nd line), :

#comment1 ##ome executable code #comment2 ##ome executable code 

but want output :

#comment1 ##some executable code #comment2 ##some executable code 

please suggest way this.

use sed 's/^[^#]/##&/'.

the & in replacement part means 'what matched'.

there's no need g when search anchored @ start of line; can match once on given line. otoh, doesn't damage.


i note input file:

#comment1   executable code #comment2  executable code 

your original output have been:

#comment1  ##some executable code #comment2 ##some executable code 

because leading space have been lost. revised script, output preserve leading space:

#comment1  ## executable code #comment2 ## executable code 

Comments

Popular posts from this blog

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

html - Outlook 2010 Anchor (url/address/link) -

android - How to create dynamically Fragment pager adapter -