0

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?

Greetz

1 Answer 1

0
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
6
  • OK it's a bit late here... I'm confused ^^ Can you explain further please
    – diggidre
    Commented Mar 14, 2019 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
    Commented Mar 14, 2019 at 23:24
  • Ok sry I had to work on another few things. So here's the thing. But what if this certain string an a row exists in many other rows in that file? Then all bravo would be replaced with delta. How could I tell sed to only handle this certain row? Example row: abc blah texttochange def blah texttochange blahblah ABC is a static fixed part that never changes so could be possibility for a sed trigger
    – diggidre
    Commented Mar 25, 2019 at 11:21
  • Ok sry I had to work on another few things. So here's the thing. But what if this certain string in a row exists in many other rows in that file? Then all bravo would be replaced with delta. How could I tell sed to only handle this certain row? Example row: # abc blah texttochange def blah texttochange blahblah # ABC is a static fixed part that never changes so could be used as a trigger DEF is some random variable text and changes from time to time.
    – diggidre
    Commented Mar 25, 2019 at 11:26
  • Sry for the double but the 5 minute timer got me trapped :)
    – diggidre
    Commented Mar 25, 2019 at 11:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .