0

In sed documentation, we read that "sed works by making only one pass over the input(s)".

I would like to write a sed script (to be run like this : sed -f my_script.sed), that performs several operations including on the operations that were performed earlier in the script.

Is it possible to perform a second (or n-th) pass on the edited stream content ?

e.g. :

  • initial stream content :

titi

  • stream content after first pass :
/titi/ i (
/titi/ a )
$ cat test.txt
(
titi
)
  • after another pass :
s/(/{/g
s/)/}/g

I would like the output to be

$ cat test.txt
{
titi
}

This is just an example to show that i'd like to perform stream editions that have effects on previous commands inside a single .sed script.

0
1

sed only gets one pass over the input stream. This is specifically stated in the man page (at least, on Fedora 33, anyway).

Whenever I have to do something like this, I pipe the output of sed into a second instance of sed to do the subsequent edits.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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