Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add or remove character from a specified line number in shell?

Tags:

bash

shell

unix

sed

I have a file which I must iterate through several times, running it with python each time. How can I remove or add characters from specific lines in shell?

for an example file ex.file,

$ cat ex.file
stuff
more stuff
more stuff
more stuff
#<- remove this character
<-add a pund sign here
more stuff
more stuff

my desired output would be:

$ cat ex.file
stuff
more stuff
more stuff
more stuff
<- remove this character
#<-add a pund sign here
more stuff
more stuff
like image 888
kilojoules Avatar asked Mar 12 '23 11:03

kilojoules


1 Answers

To add a # at the start of line 6:

sed '6 s/^/#/' ex.file

To remove the leading # from line 5:

sed '5 s/^#//' ex.file
like image 124
John Zwinck Avatar answered Mar 15 '23 22:03

John Zwinck