Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace specific line numbers using sed

Tags:

sed

I use below to replace text on line numbers 29 to 32:

sed -i '29,32 s/^ *#//' file

How can I further add line numbers i.e. line 35 to 38, line 43 & line 45 in above command?

like image 334
S R Avatar asked Oct 19 '25 13:10

S R


1 Answers

With GNU sed. m is here a label.

sed -i '29,32bm;35,38bm;43bm;45bm;b;:m;s/^ *#//' file

From man sed:

b label: Branch to label; if label is omitted, branch to end of script.

: label: Label for b and t commands.

like image 77
Cyrus Avatar answered Oct 22 '25 04:10

Cyrus



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!