You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Inserting a multi-line text after a match is not easy with sed.
Example:
$ bat file.txt
───────┬─────────────────────────────────────
│ File: file.txt
───────┼─────────────────────────────────────
1 │ first line
2 │ insert after this line
3 │ last line
───────┴─────────────────────────────────────
$ echo"$newtext"
first new line
second new line
$ bat file.txt | sed "/^insert after this line$/a $newtext"
first line
insert after this line
first new line
last line
Wrong: The first line of the inserted text is not indented. The second line of the inserted text is missing.
This way it works:
$ bat file.txt | sed "/^insert after this line/a \ \ first new line\n second new line"
first line
insert after this line
first new line
second new line
last line
Other idea:
$ bat file.txt | sed "s/(^insert after this line$)/\1$newtext/"
sed: -e expression #1, char 47: unterminated `s' command
sd solution
$ bat file.txt | sd "(^insert after this line$)""\$1\n$newtext"
first line
insert after this line
first new line
second new line
last line
Better than with sed but it could be easier.
Idea:
$ bat file.txt | sd --after "^insert after this line$""$newtext"
And analogous:
$ bat file.txt | sd --before "^insert before this line$""$newtext"
Do you think this would be useful? Or would you use the "\$1\n$newtext" solution in such a case?
The text was updated successfully, but these errors were encountered:
Inserting a multi-line text after a match is not easy with sed.
Example:
Wrong: The first line of the inserted text is not indented. The second line of the inserted text is missing.
This way it works:
Other idea:
sd solution
Better than with sed but it could be easier.
Idea:
And analogous:
Do you think this would be useful? Or would you use the
"\$1\n$newtext"
solution in such a case?The text was updated successfully, but these errors were encountered: