Hi Assaf, Thanks for the explanation. But if I put 'i' command before 'd' command, shouldn't the input new lines be deleted ? Since new inputed lines contain the pattern 'd' command want. Br, Xingming 2017-06-14 22:58 GMT+08:00 Assaf Gordon : > tag 27354 notabug > thanks > > Hello, > On Wed, Jun 14, 2017 at 11:48:41AM +0800, Han Lu wrote: > [...] > >> The below command works as expected. >> ~$ sed -e 's/127.0.0.1/127/' -e "1i 127.0.0.1 localhost\n127.0.0.1 >> > [...] > >> But command below is not working as expeced: >> ~$ sed -e '/127.0.0.1/d' -e "1i 127.0.0.1 localhost\n127.0.0.1 >> > > First, > thank you for providing such clear information on how to > reproduce your output - it makes troubleshooting much easier. > > This is not a bug in sed, but a side-effect of the 'd' (=delete) > command: when sed executes the 'd' command, it immediately starts > a new cycle (i.e. skips all other commands, reads the next line of the > input, and starts from the first sed command). > > In your example, the sequence is: > 1. sed reads the first input line (it contains '127.0.0.1') > 2. the 'd' command is executed - line is deleted and not printed, > and sed restarts a new cycle: > it skips all other commands and reads the next input line. > 4. The first time the '1i' command is executed, sed is already > on the second input line, and the address '1' is never matched - > the command is never executed. > > This also explains why it does work when you add a dummy comment line: > The '/127.0.0.1/d' does not match the first line, > and then the '1i' command is matched and executed. > > A simple work-around would be to put the 'i' command > before the 'd' command, like so: > > sed -e '1i 127.0.0.1 ....' -e '/127/d' > > As such, I'm marking this as 'not a bug', > but discussion can continue by replying to this thread. > > regards, > - assaf > > > >