Issue
I have the following very long file:
...
close unit 1
...
...
close unit 1
...
...
close unit 1
stop
I want to insert multiples lines before the last close unit 1
which is before stop
. The file contains an undefined number of close unit 1
.
I found a lot of other similar questions here and there, but the answers couldn't help me... For example I tried https://stackoverflow.com/a/8635732/1689664 but this didn't work...
Solution
Using sed
and tac
:
$ tac inputfile | sed '/close unit 1/ {s/\(.*\)/\1\nLine3\nLine2\nLine1/; :loop; n; b loop}' | tac
...
close unit 1
...
...
close unit 1
...
...
Line1
Line2
Line3
close unit 1
stop
Note that you'd need to specify the input lines in the reverse order in the sed
expression.
Answered By - devnull