kilojoules
kilojoules

Reputation: 10093

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

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

Upvotes: 5

Views: 3924

Answers (2)

P....
P....

Reputation: 18411

Oneliner awk version to remove # from 5th line and add # to 6th line.

awk 'NR==5 {sub(/^#/,"") } NR==6 { sub(/^/,"#")}1' infile

Upvotes: 2

John Zwinck
John Zwinck

Reputation: 249502

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

Upvotes: 7

Related Questions