From unknown Sun Jun 22 07:52:12 2025 X-Loop: help-debbugs@gnu.org Subject: bug#47390: -i and -n options are order-sensitive Resent-From: rgrosso Original-Sender: "Debbugs-submit" Resent-CC: bug-sed@gnu.org Resent-Date: Thu, 25 Mar 2021 15:21:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 47390 X-GNU-PR-Package: sed X-GNU-PR-Keywords: To: 47390@debbugs.gnu.org X-Debbugs-Original-To: Received: via spool by submit@debbugs.gnu.org id=B.16166856419291 (code B ref -1); Thu, 25 Mar 2021 15:21:02 +0000 Received: (at submit) by debbugs.gnu.org; 25 Mar 2021 15:20:41 +0000 Received: from localhost ([127.0.0.1]:39247 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1lPRmp-0002Pm-Qh for submit@debbugs.gnu.org; Thu, 25 Mar 2021 11:20:40 -0400 Received: from lists.gnu.org ([209.51.188.17]:51626) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1lPMV6-0005Qk-7v for submit@debbugs.gnu.org; Thu, 25 Mar 2021 05:42:00 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:37132) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lPMV6-0006OT-0u for bug-sed@gnu.org; Thu, 25 Mar 2021 05:42:00 -0400 Received: from lxmtout2.gsi.de ([140.181.3.112]:41540) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lPMV4-00020Q-0k for bug-sed@gnu.org; Thu, 25 Mar 2021 05:41:59 -0400 Received: from localhost (localhost [127.0.0.1]) by lxmtout2.gsi.de (Postfix) with ESMTP id DFA86202AD6A for ; Thu, 25 Mar 2021 10:41:51 +0100 (CET) X-Virus-Scanned: Debian amavisd-new at lxmtout2.gsi.de Received: from lxmtout2.gsi.de ([127.0.0.1]) by localhost (lxmtout2.gsi.de [127.0.0.1]) (amavisd-new, port 10024) with LMTP id qnE6GF3XOWQK for ; Thu, 25 Mar 2021 10:41:51 +0100 (CET) Received: from srvex3.campus.gsi.de (srvex3.campus.gsi.de [10.10.4.16]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-SHA256 (128/128 bits)) (No client certificate requested) by lxmtout2.gsi.de (Postfix) with ESMTPS id C8762202AD5D for ; Thu, 25 Mar 2021 10:41:51 +0100 (CET) Received: from [192.168.178.31] (140.181.3.12) by srvex3.campus.gsi.de (10.10.4.16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.2106.2; Thu, 25 Mar 2021 10:41:51 +0100 From: rgrosso Message-ID: <80eeb7cc-47de-2678-e881-a5fb91f29aec@gsi.de> Date: Thu, 25 Mar 2021 10:41:51 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.8.1 MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8"; format=flowed Content-Transfer-Encoding: quoted-printable Content-Language: en-US X-Originating-IP: [140.181.3.12] X-ClientProxiedBy: srvex4.Campus.gsi.de (10.10.4.36) To srvex3.campus.gsi.de (10.10.4.16) Received-SPF: pass client-ip=140.181.3.112; envelope-from=R.Grosso@gsi.de; helo=lxmtout2.gsi.de X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_DNSWL_NONE=-0.0001, SPF_PASS=-0.001, T_SPF_HELO_PERMERROR=0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-Spam-Score: -1.3 (-) X-Mailman-Approved-At: Thu, 25 Mar 2021 11:20:39 -0400 X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -2.3 (--) Hi, To remove the first N lines from a big text file I used to do: sed -in '1,Nd' bigfile.txt and it was working as expected. Assuming the order of the flags does not = matter I did once: sed -ni '1,Nd' bigfile.txt which deleted the whole content of the file. Is this behaviour expected? = =46rom the user point of view this seems like a rather dangerous bug. I=20 think, flags not taking an argument should be insensitive to the order. Currently, to get the same result without running the risk to forget the = right order of the flags and deleting the file content, I do: sed -i -e '1,Nd' bigfile.txt (Using GNU sed 4.4 or 4.8) Cheers, =C2=A0=C2=A0 Raffaele From unknown Sun Jun 22 07:52:12 2025 X-Loop: help-debbugs@gnu.org Subject: bug#47390: -i and -n options are order-sensitive References: <80eeb7cc-47de-2678-e881-a5fb91f29aec@gsi.de> In-Reply-To: <80eeb7cc-47de-2678-e881-a5fb91f29aec@gsi.de> Resent-From: Nora Platiel Original-Sender: "Debbugs-submit" Resent-CC: bug-sed@gnu.org Resent-Date: Wed, 28 Apr 2021 21:10:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 47390 X-GNU-PR-Package: sed X-GNU-PR-Keywords: To: 47390@debbugs.gnu.org, rgrosso Received: via spool by 47390-submit@debbugs.gnu.org id=B47390.161964418310920 (code B ref 47390); Wed, 28 Apr 2021 21:10:01 +0000 Received: (at 47390) by debbugs.gnu.org; 28 Apr 2021 21:09:43 +0000 Received: from localhost ([127.0.0.1]:54208 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1lbrRG-0002q4-Ml for submit@debbugs.gnu.org; Wed, 28 Apr 2021 17:09:42 -0400 Received: from mout.gmx.net ([212.227.17.20]:34935) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1lbrRC-0002pp-LK for 47390@debbugs.gnu.org; Wed, 28 Apr 2021 17:09:41 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.net; s=badeba3b8450; t=1619644171; bh=4UxNsV0Z+kc5tuggSik7mRkN+HoRlNo1CmzBKN6uQLU=; h=X-UI-Sender-Class:From:To:Subject:Date; b=kokV2FD7Xnz7O0CgHpTmyZezetufFUhJ/9Vjo321VRs5Iz5LD3VGw2mncAbnOX0Un rbMuY1OjtUVklqgR6Wfe/4IbbtMdnTBjOjCKxo8HBoiEQWPr6hBOCKx1zDGGag4KNd sfj91ATyPyCd96qRtvnFTnmVWH7W5DQ9nfOuRKLo= X-UI-Sender-Class: 01bb95c1-4bf8-414a-932a-4f6e2808ef9c Received: from [82.53.198.188] ([82.53.198.188]) by web-mail.gmx.net (3c-app-mailcom-bs13.server.lan [172.19.170.181]) (via HTTP); Wed, 28 Apr 2021 23:09:31 +0200 MIME-Version: 1.0 Message-ID: From: Nora Platiel Content-Type: text/plain; charset=UTF-8 Date: Wed, 28 Apr 2021 23:09:31 +0200 Importance: normal Sensitivity: Normal X-Priority: 3 X-Provags-ID: V03:K1:DRm46gEw9DlV/4G9ghX4sszX9EV9K2DBhKBNRPlUsrBItW4i3rWL7q6bfPqk9MBejMFzz jjt7vIY7cpZCrLyM/G9vomNo1YLWFdKFCavq2Cpu0QsHY0y2COHZaRgTJGq0URnNJmbMl1wIlILa keueF6q7hATO2I6I8nGZF278Vb8RKKRohgXMkmvEzSccFdP2B5rF4MlVTIA5IvsfFX9GE+PQ+PZC MJELKrCqUXOibEErh8hEMnSb6LiPdaAGLn9UNs1sW7DADw6FROQeGMSVzcrDJcbhzYCmzo9toKxB mQ= X-Spam-Flag: NO X-UI-Out-Filterresults: notjunk:1;V03:K0:9UVUHJenfok=:9SX7dn1g60gaefbjqC8AtI 4l00h4dCfL1gobQD1ty0IBjkf+X4ctm+Lnq6/wW58cmCR7IVrb/EB+mWJi0VtyF0yCFQ9r5Z7 VGgVuebuxCNxxZ/ZImtg6Dn8sQfny1RvXjgNPbvYCrZC9ndJqJXsJI59pTquocn8Lci4HHHue RgUrWzcfkVQuafT9QH7mOjTTthGiC/a2BM8CoDEZ9HYF3OBkc18jpQLe2aqodYrsbf1uSPLD9 rTZyaGFvizw+/9kER2y455O67tiE3tRjutbBP9hY1PoESOS28+e3UBujbdy5ZbCz4efWd/JDE rL0iKnXlYgjD3ChWt/a8TJiFIn3wJr67Zx9cHad88oCUj5AhNHHGgI2c0X/tz4Fybu/9EgY3v hWm9YK9ZyQgkFBw2+E1dLNHxw0uqileIz5m6fMz0qv4xnPJsrsyq4y+1og9P9jksj9NtPwHTN gLCXqrzO3Gj5GoMEfAjOTLjwAi2WmzRNZU7nogDYjYMqi8PhkGv4zod+cmcYeBdGhKd3cy1vP bebiIG1AiN5BSmvp3bj/PmVJ/8z/yVP+V8ynvhkHZQtmdJuwTtvqKapwF+hC5RFt7egOjLJS5 Irwr5jTSGfWsDcuqq2K3fECs9NBuhyWgpJIQ3lAVVuXTWqgg1lISlctcerLKfsIKK0RXiXzQL EHhYhlFBGZ8uCWj91Ee4oz1+/FL/be4ymmVGqGFKgzcAJT05HfWR2bXgVsxXp5C+vTrD1OWkO idr6+DKpH3oYvhxE4af4BUFs9KYm7PDuqQfQDpmAkGIUh+FAYlPx+Dl84ll0JEHZXQeNI86xy ga2wM8W4LK2Qt4gYXj3gwkt9ZrZJbl+UDLo7RHkXJbtBtNDy8Mtl0J6R/Qulb2lXt123CAh74 aJfzfm0CuhG6gj+DsfxZIRu+cjGXKKJkiKljNZAoK3LZYyNpQN5M8obPeyF97H Content-Transfer-Encoding: quoted-printable X-Spam-Score: -0.7 (/) X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -1.7 (-) Hello, they are not order-sensitive, but the -i flag takes an optional argument (= SUFFIX), therefore it cannot be grouped together with other short flags un= less it is the last one. > To remove the first N lines from a big text file I used to do: > > sed -in '1,Nd' bigfile.txt > > and it was working as expected. In this invocation, the "n" is not a flag, it is the argument to the -i fl= ag. Same as: sed --in-place=3Dn '1,Nd' bigfile.txt which tells sed to use "n" as the backup suffix (creating "bigfile.txtn" a= s backup of "bigfile.txt"). > Assuming the order of the flags does not matter I did once: > > sed -ni '1,Nd' bigfile.txt > > which deleted the whole content of the file. Is this behaviour expected? In this invocation, you are passing both the -n and -i flags (the latter w= ith no argument, which disables backup). Same as: sed --quiet --in-place '1,Nd' bigfile.txt The output file will be empty as expected, because -n/--quiet disables aut= omatic printing and you are not using any printing command in your sed scr= ipt. > From the user point of view this seems like a rather dangerous bug. The -i flag of sed is well documented, and the docs include a warning for = the very same pitfall you encountered: https://www.gnu.org/software/sed/manual/sed.html#Command_002dLine-Options You may think that flags with optional arguments are error prone (and I wo= uld agree), but there's nothing we can do about it here. GNU flags have wo= rked like this for a long time. > I think, flags not taking an argument should be insensitive to the order= . Usually they are, unless they are exclusive and the last one takes effect,= but this is not the problem here. > Currently, to get the same result without running the risk to forget the > right order of the flags and deleting the file content, I do: > > sed -i -e '1,Nd' bigfile.txt This has yet a different effect: the -i flag is without argument (no backu= p) and the -n is not present (autoprinting is on). The -e makes no differe= nce in this case because there is only one script. If you want the "-in" e= ffect, the only alternative is "--in-place=3Dn". Regards, NP From unknown Sun Jun 22 07:52:12 2025 X-Loop: help-debbugs@gnu.org Subject: bug#47390: -i and -n options are order-sensitive Resent-From: rgrosso Original-Sender: "Debbugs-submit" Resent-CC: bug-sed@gnu.org Resent-Date: Thu, 29 Apr 2021 13:24:03 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 47390 X-GNU-PR-Package: sed X-GNU-PR-Keywords: To: Nora Platiel , <47390@debbugs.gnu.org> Received: via spool by 47390-submit@debbugs.gnu.org id=B47390.161970263025140 (code B ref 47390); Thu, 29 Apr 2021 13:24:03 +0000 Received: (at 47390) by debbugs.gnu.org; 29 Apr 2021 13:23:50 +0000 Received: from localhost ([127.0.0.1]:55112 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1lc6dv-0006XL-AP for submit@debbugs.gnu.org; Thu, 29 Apr 2021 09:23:50 -0400 Received: from lxmtout1.gsi.de ([140.181.3.111]:51182) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1lc45o-0000At-8i for 47390@debbugs.gnu.org; Thu, 29 Apr 2021 06:40:25 -0400 Received: from localhost (localhost [127.0.0.1]) by lxmtout1.gsi.de (Postfix) with ESMTP id 9AEDA2050D0C; Thu, 29 Apr 2021 12:40:17 +0200 (CEST) X-Virus-Scanned: Debian amavisd-new at lxmtout1.gsi.de Received: from lxmtout1.gsi.de ([127.0.0.1]) by localhost (lxmtout1.gsi.de [127.0.0.1]) (amavisd-new, port 10024) with LMTP id jFvSA1_DPZyF; Thu, 29 Apr 2021 12:40:17 +0200 (CEST) Received: from srvex1.campus.gsi.de (srvex1.campus.gsi.de [10.10.4.11]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-SHA256 (128/128 bits)) (No client certificate requested) by lxmtout1.gsi.de (Postfix) with ESMTPS id 82F762050D00; Thu, 29 Apr 2021 12:40:17 +0200 (CEST) Received: from localhost (140.181.3.12) by srvex1.campus.gsi.de (10.10.4.11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.2242.4; Thu, 29 Apr 2021 12:40:17 +0200 References: From: rgrosso Message-ID: <7abcb096-1908-f5fc-00cc-e465f9426298@gsi.de> Date: Thu, 29 Apr 2021 12:40:27 +0200 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset="utf-8"; format=flowed Content-Transfer-Encoding: quoted-printable Content-Language: en-US X-Originating-IP: [140.181.3.12] X-ClientProxiedBy: srvex3.Campus.gsi.de (10.10.4.16) To srvex1.campus.gsi.de (10.10.4.11) X-Spam-Score: -0.0 (/) X-Mailman-Approved-At: Thu, 29 Apr 2021 09:23:46 -0400 X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -1.0 (-) Hello Nora, thanks a lot for the explanations and sorry for the noise. Indeed, as=20 you said, the manual is clear about both pitfalls: - that the '-i' should not be followed by any other short option in=20 general and - that '-n' together with '-i' disables printing in the file so to say. Thanks again for your time and for the extensive answer (I guess you=20 might have been tempted by a much shorter "RTFM" answer :-) ) Best regards, have a nice day, =C2=A0=C2=A0 Raffaele On 4/28/21 11:09 PM, Nora Platiel wrote: > Hello, > they are not order-sensitive, but the -i flag takes an optional argumen= t (SUFFIX), therefore it cannot be grouped together with other short flag= s unless it is the last one. > >> To remove the first N lines from a big text file I used to do: >> >> sed -in '1,Nd' bigfile.txt >> >> and it was working as expected. > In this invocation, the "n" is not a flag, it is the argument to the -i=20 flag. > Same as: > sed --in-place=3Dn '1,Nd' bigfile.txt > which tells sed to use "n" as the backup suffix (creating "bigfile.txtn= " as backup of "bigfile.txt"). > >> Assuming the order of the flags does not matter I did once: >> >> sed -ni '1,Nd' bigfile.txt >> >> which deleted the whole content of the file. Is this behaviour expecte= d? > In this invocation, you are passing both the -n and -i flags (the latte= r with no argument, which disables backup). > Same as: > sed --quiet --in-place '1,Nd' bigfile.txt > The output file will be empty as expected, because -n/--quiet disables = automatic printing and you are not using any printing command in your sed=20 script. > >> From the user point of view this seems like a rather dangerous bug. > The -i flag of sed is well documented, and the docs include a warning f= or the very same pitfall you encountered: > https://www.gnu.org/software/sed/manual/sed.html#Command_002dLine-Optio= ns > You may think that flags with optional arguments are error prone (and I=20 would agree), but there's nothing we can do about it here. GNU flags have=20 worked like this for a long time. > >> I think, flags not taking an argument should be insensitive to the ord= er. > Usually they are, unless they are exclusive and the last one takes effe= ct, but this is not the problem here. > >> Currently, to get the same result without running the risk to forget t= he >> right order of the flags and deleting the file content, I do: >> >> sed -i -e '1,Nd' bigfile.txt > This has yet a different effect: the -i flag is without argument (no ba= ckup) and the -n is not present (autoprinting is on). The -e makes no dif= ference in this case because there is only one script. If you want the "-= in" effect, the only alternative is "--in-place=3Dn". > > Regards, > NP >