sed command to replace a line at a specific line number with some other line

my requirement is,

consider a file output

cat output
 
blah sdjfhjkd jsdfhjksdh
sdfs 23423 sdfsdf sdf"sdfsdf"sdfsdf"""""dsf
 hellow there 
this doesnt look good
et cetc etc
etcetera

i want to replace a line of line number 4 ("this doesnt look good") with some other line

say echo "me there $chco" (with the echo command)

the below is not doing the job... i dont watnt to replace a string. All i need is it should delete line 4 and replace my whole sentence in that line

 
 sed -i '4 s|*|echo \"me there \$ech\" |'  output

PS: i dont want to perform any file redirection either
any help is appreciated thanks

With awk

awk 'NR==4 {$0="me there $chco"} 1' file
blah sdjfhjkd jsdfhjksdh
sdfs 23423 sdfsdf sdf"sdfsdf"sdfsdf"""""dsf
 hellow there
me there $chco
et cetc etc
etcetera
1 Like
$ sed '4 c \"me there \$ech\"' output
blah sdjfhjkd jsdfhjksdh
sdfs 23423 sdfsdf sdf"sdfsdf"sdfsdf"""""dsf
 hellow there
"me there $ech"
et cetc etc
etcetera
$ sed '4 s/.*/\"me there \$ech\"/' output
blah sdjfhjkd jsdfhjksdh
sdfs 23423 sdfsdf sdf"sdfsdf"sdfsdf"""""dsf
 hellow there
"me there $ech"
et cetc etc
etcetera

You can add the -i flag. :slight_smile:

1 Like

thanks guys.. works good :slight_smile: