Timeline for sed: delete all lines before matching one, including this one
Current License: CC BY-SA 4.0
18 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Aug 9, 2019 at 11:35 | comment | added | Philippos | Thank you for bringing this up, @MichaelHomer ! | |
| Aug 9, 2019 at 7:58 | history | edited | Michael Homer | CC BY-SA 4.0 |
added 19 characters in body
|
| Aug 9, 2019 at 7:56 | comment | added | Michael Homer |
@Philippos I have posted a question about this. It comes up with something as simple as 1d;1,2d with the same divergence of behaviour between implementations.
|
|
| Aug 8, 2019 at 21:15 | comment | added | user232326 |
A sed -e '1,/HI/d' file fails if the first line match the regex. A sed -e '1{/HI/d;};1,//d' file will remove up to the second regex match or the whole file if no second match exists. A sed '/HI/,$!d;//d' file will remove all lines where the regex match.
|
|
| Aug 2, 2019 at 13:35 | comment | added | Philippos | I suggest to start a question of its own out of this finding | |
| Aug 2, 2019 at 9:13 | comment | added | Michael Homer |
I'm still not sure which is wrong, but as a minimal case printf 'a\nb\n'|sed -e '1{d;};1,/a/p' behaves how I expect on BSD/macOS sed and Solaris' POSIX sed (one output line), and not on GNU sed (two output lines), including in its POSIX mode. Busybox matches GNU sed. I will try to have a prod at more exotic systems. There is an argument for either interpretation of the specification text and I'm no longer certain which one I expect from it. Presumably Solaris and macOS have passed the conformance suites, though, so that ought to be correct. Thanks for the comments! I've just edited yours in.
|
|
| Aug 2, 2019 at 9:01 | history | edited | Michael Homer | CC BY-SA 4.0 |
deleted 150 characters in body
|
| Aug 2, 2019 at 8:55 | comment | added | Michael Homer | Interesting. I read that as a conformance bug, but the "Addresses" section is a little ambiguous. It's not what BSD nor SunOS xpg4 sed does. That may be a specification issue to be raised. | |
| Aug 2, 2019 at 8:54 | comment | added | Philippos |
sed '/HI_THERE/,$!d;//d' would be a valid POSIX solution
|
|
| Aug 2, 2019 at 8:50 | comment | added | Philippos |
@MichaelHomer Yes, it does, I just veryfied it with GNU sed, no output. After deleting the first line, second cycle it started, finds itself in the range between first line and pattern and deletes.
|
|
| Aug 2, 2019 at 8:50 | comment | added | Michael Homer | I have found a theoretically-POSIX-compatible version in Solaris 10 that doesn't work at all on that command, though, so investigating that... | |
| Aug 2, 2019 at 8:46 | comment | added | Michael Homer |
@Philippos Will they? Not on any system I can find, and the specified meaning of d is "delete and start the next cycle", so I can't see how 1,//d ever even runs if the first line matches.
|
|
| Aug 2, 2019 at 8:43 | comment | added | Philippos |
@MichaelHomer This POSIX version will not help, I'm afraid. All lines will get killed by the 1,//d anyhow.
|
|
| Aug 2, 2019 at 8:22 | history | edited | Michael Homer | CC BY-SA 4.0 |
added 463 characters in body
|
| Aug 2, 2019 at 8:19 | comment | added | Michael Homer |
POSIX-compatibly you can use sed -e '1{/HI_THERE/d;};1,//d' if that is an issue.
|
|
| Aug 2, 2019 at 8:12 | vote | accept | taalf | ||
| Aug 2, 2019 at 8:08 | comment | added | steeldriver |
IIRC this will fail to match (hence deleting to the end of the file) if HI_THERE occurs in line 1 (in GNU sed, one can use 0,/HI_THERE/ to cover this special case)
|
|
| Aug 2, 2019 at 8:02 | history | answered | Michael Homer | CC BY-SA 4.0 |