Search code examples
sedrangeline

Why does sed delete the wrong range of lines


Im trying to write a script that deletes a certain range of lines using the sed command. I first printed out the lines.

    sed -n '482,486p' original.json
                {
                    "vlans": "ALL",
                    "hostname": "hostname",
                    "interface": "interface"
                },

After confirming that those are the lines I executed the following code

sed '482,486d' original.json > new.json

When I ran the diff command I got the following results

diff original.json new.json
484,488d483
<                     "hostname": "hostname",
<                     "interface": "interface"
<                 },
<                 {
<                     "vlans": "ALL",

My question is why was the range of lines from 484-448 deleted when I specified lines 482-486 to be deleted and how do I fix it. Any help would be appreciated


Solution

  • You haven't given us enough information to reproduce the error, but I'll go out on a limb.

    My guess is that lines 487 and 488 of the original file are

    {
        "vlans": "ALL",
    

    So if we put the original and the new side by side, it looks like this:

    481 ...     481 ...
    482 {       487 {
    483 vlans   488 vlans
    484 host    489 ...
    485 int
    486 }
    487 {
    488 vlans
    489 ...
    

    So lines 482-486 were excised from original, but diff sees everything matching up through line 483, then five lines 484-488 in original not appearing in new, and then all the rest matching.