Bug 247930 - sed pattern space deleted upon leading insertion
Summary: sed pattern space deleted upon leading insertion
Status: New
Alias: None
Product: Base System
Classification: Unclassified
Component: bin (show other bugs)
Version: 12.1-RELEASE
Hardware: Any Any
: --- Affects Only Me
Assignee: freebsd-bugs (Nobody)
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2020-07-12 11:19 UTC by Quentin L'Hours
Modified: 2020-07-12 11:19 UTC (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Quentin L'Hours 2020-07-12 11:19:23 UTC
When adding leading text with `s/^/some text/`, if the pattern space contains
multiple lines with the first one being empty then the pattern space remaining
lines are deleted (case 2 below).

One line reproducer:
```
sh$ echo content | sed 'H; g; s/^/preceding/'
preceding

sh$
```

More complete examples:

1 - Non empty first line case

```
s/.*/1st line\
2nd line\
3rd line/

l
i \
-----

s/^/some preceding text /

l
```

```
sh$ echo | sed -nf non-empty-first-line.sed
1st line$
2nd line$
3rd line$
-----
some preceding text 1st line$
2nd line$
3rd line$
```

2 - Empty first line case (the actual bug)

```
s/.*/\
2nd line\
3rd line/

l
i \
-----

s/^/some preceding text /

l
```

```
sh$ echo | sed -nf empty-first-line.sed
$
2nd line$
3rd line$
-----
some preceding text $
$
```

3 - Empty first line case workaround

```
s/.*/\
2nd line\
3rd line/

l
i \
-----

s/.*/some preceding text &/

l
```

```
sh$ echo | sed -nf empty-first-line-workaround.sed
$
2nd line$
3rd line$
-----
some preceding text $
2nd line$
3rd line$
```