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

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

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -