I tried to use sed to edit some part of a specific line in a text file and save the change to the original file. OK I now understand that this is not the way sed directly works. But how can I use sed in a script to edit the part of a line with variable content in it? I already used sed to change complete lines or used lines as trigger to cut and insert to a new file. But this is different :) I think what I need is

sed '/baz/s/foo/bar/g'

sed '/beginning of line/s/static text/new static text/g'

This gives me the right output but how can I save it to the source file? I understand that sed outputs to stoud but when I add a ">> outpufile" it gives me an error message

sed: -e Ausdruck #1, Zeichen 55: Unbekannte Option für `s'

Between static text and new static text is some content that changes from time to time so

sed -n '/static text/new static text/,$p' tmp >> source

will work only for as long as the source content hasn't changed.

Any suggestions?


micha@linux-micha: /tmp
$ echo 'alpha bravo charlie' > blah.txt

micha@linux-micha: /tmp
$ cat blah.txt
alpha bravo charlie

micha@linux-micha: /tmp
$ sed -i 's/bravo/delta/' blah.txt

micha@linux-micha: /tmp
$ cat blah.txt
alpha delta charlie
  • OK it's a bit late here... I'm confused ^^ Can you explain further please – diggidre Mar 14 at 23:10
  • Hi, thought you meant in-place editing a file using sed. That's possible with the -i or --in_place flag. See it in my 3rd command above? Regards. – Micha Mar 14 at 23:24

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.