From debbugs-submit-bounces@debbugs.gnu.org Mon Sep 20 10:57:31 2021 Received: (at submit) by debbugs.gnu.org; 20 Sep 2021 14:57:31 +0000 Received: from localhost ([127.0.0.1]:43807 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1mSKja-0007B1-Vj for submit@debbugs.gnu.org; Mon, 20 Sep 2021 10:57:31 -0400 Received: from lists.gnu.org ([209.51.188.17]:40494) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1mSF3k-0000sU-OT for submit@debbugs.gnu.org; Mon, 20 Sep 2021 04:54:00 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:57034) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mSF3k-0000TJ-Eh for bug-sed@gnu.org; Mon, 20 Sep 2021 04:53:56 -0400 Received: from hale.su ([2a01:4f8:c0c:66d3::69]:41012 helo=lifuin.ran.hale.su) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mSF3i-0007HR-QZ for bug-sed@gnu.org; Mon, 20 Sep 2021 04:53:56 -0400 Received: from hale.su (unknown [IPv6:fdd9:80f2:e3::7056:8e4]) by lifuin.ran.hale.su (Postfix) with ESMTPSA id CE60D104398 for ; Mon, 20 Sep 2021 10:53:50 +0200 (CEST) Date: Mon, 20 Sep 2021 10:53:48 +0200 From: lexi hale To: bug-sed@gnu.org Subject: cannot append or insert to empty file or stream Message-ID: <20210920105348.1ae1bcc5@hale.su> X-Image-URL: https://hale.su/me.jpeg Face: iVBORw0KGgoAAAANSUhEUgAAADAAAAAwBAMAAAClLOS0AAAAFVBMVEXGyahMGiWZX1hAS0V5W0/VYVYOEiXrPhFGAAACcklEQVQ4jVXUQZfiIAwA4Czauaci5xphzmp9PQ8U99zhlft29fH/f8ImVOe53ORrEkhTIYZjWRfGXZhgXcrCG4RzzG9Av59wDR5vL4hvcAvxrF8BEQINz0w3Z+OzCMUI9IQl3vK8guL98SdCU0gqBqlOFairNXQgS9mGaZIC70CZrGOoiWLsIcBnWUppc850S2FKsa4rEMgFl8CQoiM+QA3ooWNYkKHJs9eUjuu+wKaUM6LL+YAYkpfK/RkZtgJn7Rrf+5b8NY6mL09Abc6By9prO15Hc/UoqVQxqHcHnfl6o45jCRZ3VwFjlgWjn7jMjGG8pHHHveJUho/VRpqaPM2oY7ZjX0Gh4ZfksvTvG8lRHGMFK3DjCzIcogAv4pvbqy+a9yXCkm0ZyHbcY2U9Yl4lRmxTDOSk+dxPLPs1AhcdEvGeQGfJl7+1RsMHpORoqsBvSJulpvrF4NZjcNZgic+1l1TfK9SBGMqFATkEKpSuWSelFIng1fLvGXHpGniH5QWm481mUlDudHC+/ESYOqXdAOWRD3t+dxH50VlrvxewBUrI8wpOIPo6vuoID8ppbxG1DCHPKKIU5+gL5eys18h9nFIgj11XTyUP5sS50CXHM+rxBFU4cwWP+8SvtvXmJJkIcgVufNtmaGb0pgU4ggK5f5Pk5sTwzVd9AGwu8AZcvPEL3u9cou9WmAV4HLZYTBkUw9f6LUrztMtkBAqf92MF30p7NRnZLuWkuu2pwvkh7TWLkU+olD8wQgU13JFz8Gtc4QSb7iLwMSwMi3n+ERSlxq+twOdYP/QX8HHHU4W+X/8aXqBAfW0E7sPwH/BMweYfLV7T6AMQ+4sAAAAASUVORK5CYII= MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Received-SPF: pass client-ip=2a01:4f8:c0c:66d3::69; envelope-from=lexi@hale.su; helo=lifuin.ran.hale.su 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, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-Spam-Score: -1.4 (-) X-Debbugs-Envelope-To: submit X-Mailman-Approved-At: Mon, 20 Sep 2021 10:57:29 -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.4 (--) there is an inconsistency in the handling of the a and i commands in sed. if the input stream or file immediately yields EOL, sed ignores the commands and produces an empty stream. for instance: $ echo -n >file line-1 $ sed -i aline-2 file $ cat file correctly yields line-1 line-2 however, $ touch empty $ sed -i aline-1 empty $ cat empty prints nothing, instead of the expected result "line-1". this is an extremely surprising behavior that limits the utility of sed when one cannot predict the contents of the file in question. if it is strictly necessary for standards conformance or to meet the expectations of ancient shell scripts that depend on nonstandard behavior, it would be helpful to at least have a flag that would turn on consistent behavior for these commands. however, making the consistent behavior the new default might also fix a few extremely rare bugs in existing shell scripts :) thanks for your time, lexi From debbugs-submit-bounces@debbugs.gnu.org Mon Sep 20 15:28:54 2021 Received: (at 50701) by debbugs.gnu.org; 20 Sep 2021 19:28:54 +0000 Received: from localhost ([127.0.0.1]:44072 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1mSOyE-0001KU-GM for submit@debbugs.gnu.org; Mon, 20 Sep 2021 15:28:54 -0400 Received: from mail-pj1-f49.google.com ([209.85.216.49]:40472) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1mSOyB-0001K7-Uo; Mon, 20 Sep 2021 15:28:53 -0400 Received: by mail-pj1-f49.google.com with SMTP id n13-20020a17090a4e0d00b0017946980d8dso197935pjh.5; Mon, 20 Sep 2021 12:28:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-language:content-transfer-encoding; bh=VM23YHXOK0LKEk11pZU3B7HoIcp8ZYK5S5S+jF+BubQ=; b=VQsJijTX7jaVDQBKBC6aQpktZvF8+xMCQJVgOiiVQRR8ruKPLDHOdfYEzqdgkrLgY9 yCIZeMTsU8il3Fo0a2Sb9E5n+LO3M3dnN0hGiXefkSLzBu+q0Oo31X6oglAb9prSeey1 JZWnLdVnsdsWep5BaOPDDCAvRXNgPwmM88K+GHLUJQ6iqW4AsCdIg4Ix39Zw2YusBRc+ ENtD5xAVlXvrJmEFsPfIVCGwWLH8YGhmcSE3Nf60MQIdiNETQ1TsPERnYW624arDG+9V disv4nQ1+pOOGTZr+aODIFVXxUOXUT0orPi/b5Ldd9HhkJYrr0mgEZ8PD9e85hJOqYXz aNCA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=VM23YHXOK0LKEk11pZU3B7HoIcp8ZYK5S5S+jF+BubQ=; b=cnBHcdHdrbwFwx4kgSFXPiAvRaWrd4NOhjsnK4EzHE5Yto7Y+//0hD/A5MmxrriBOh OvbOz20FnMLY22VQEL/OgNTcaK+xnUyBKvu1U8Rs6A+nZkob4ON3PsO5h4yn4nwK0Pq/ 0AD6X0VCqsTbtA/3elYWBvjfLasdnU6o9tnP+W5GlJACWICulAwOAFN3OF84oUWTeRgY c7vlKj3uLj3cunVvcvrXcmFMgf4u0UXR58l8OEqwu074rG/ktei1fCKoB6G2Sx30M7FA COPu23x0zV3vv50M59hHkmvgZScilRXC2QZDt+Plt0llnTNzGriq9QE+t6KCggZ+4e9h TFtA== X-Gm-Message-State: AOAM533iXtAsAY8dmEunyfJOfYjJZr75eDmtmcQsaXLrGIVKI4WUi+OD McuisI9T7X61xpsBAJk/2Pkofli7qG0= X-Google-Smtp-Source: ABdhPJwzR9raTMPsRL0wTi0H+52MYa9FhsK8YTMlQTI6Hv4V6dfvbOyxDkIdAggu5Abre7Rk87qg1w== X-Received: by 2002:a17:90a:290c:: with SMTP id g12mr687056pjd.105.1632166125348; Mon, 20 Sep 2021 12:28:45 -0700 (PDT) Received: from tomato.moose.housegordon.com (moose.housegordon.com. [184.68.105.38]) by smtp.googlemail.com with ESMTPSA id p48sm5508154pfw.160.2021.09.20.12.28.43 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 20 Sep 2021 12:28:44 -0700 (PDT) Subject: Re: bug#50701: cannot append or insert to empty file or stream To: lexi hale , 50701@debbugs.gnu.org References: <20210920105348.1ae1bcc5@hale.su> From: Assaf Gordon Message-ID: <63a761b2-5366-ba7d-c51a-016c80cc5cf4@gmail.com> Date: Mon, 20 Sep 2021 13:28:42 -0600 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.14.0 MIME-Version: 1.0 In-Reply-To: <20210920105348.1ae1bcc5@hale.su> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit X-Spam-Score: -0.1 (/) X-Debbugs-Envelope-To: 50701 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.1 (-) tag 50701 notabug close 50701 stop Hello, Thank you for providing clear reproducible example of the situation - it makes troubleshooting much easier. This is, however, not a bug - but the intended behavior: On 2021-09-20 2:53 a.m., lexi hale wrote: > there is an inconsistency in the handling of the a and i commands in > sed. if the input stream or file immediately yields EOL, sed ignores > the commands and produces an empty stream. > First, let's clarify what the files are: > $ echo -n >file line-1 > $ touch empty The file 'file' is not empty, it has one line. This line just happens to be empty (i.e. no characters in the line before the newline character). The file 'empty' is empty, it contains NO lines. The above might seem obvious, but the distinction is important for the next step: > $ sed -i aline-2 file The sed command "aline-2", means append ("a") the text "line-2", to *every* line. The "every line" parts is due to the command "a" having no address part (contrast with sed command "3aline-2" which would add "line-2" only to the third line). If the input file has (any) lines, the command will be executed. If the input does not have any lines, the command will not be executed. And that is the behavior you are seeing. > however, > > $ sed -i aline-1 empty > $ cat empty> > prints nothing, instead of the expected result "line-1". > Compare the situation to this (slightly more obvious) case: $ printf "%s\n" hello world > in1 $ cat in1 hello world Add 'FOO' after every line: $ sed 'aFOO' in1 hello FOO world FOO Add 'FOO' after the second line: $ sed '2aFOO' in1 hello world FOO Add 'FOO' after the third line: $ sed '3aFOO' in1 hello world Since there is NO third line in the input, the command wasn't executed. And similarly, since the 'empty' file does not have ANY lines, the command (which is programmed to run on every line) is not executed. > this is an extremely surprising behavior that limits the utility of sed > when one cannot predict the contents of the file in question. I hope the explanation above makes this behavior less surprising. > [...] it would be helpful to at least have a flag that would turn > on consistent behavior for these commands. > > however, making the consistent behavior the new default might also fix > a few extremely rare bugs in existing shell scripts :) I humbly think that this behavior is consistent and does not require any modification or flags - but other opinions are welcomed. When integrating with shell scripts, edge-cases (e.g. empty input) should probably be checked explicitly. Few suggestion to check for empty files (contrived and not tested): if test -s INPUTFILE ; then # process the file else echo "FILE IS EMPTY" fi --- lines=$(wc -l < INPUTFILE) if test "$lines" -gt 0 ; then # process the file else echo "FILE IS EMPTY" fi And lastly, since you are already using GNU sed extensions (and not worrying about portability), you can use the 'qNUM' command extension to exit with a specific code if there was any input: sed -e 'aline-1' -e '$q42' INPUTFILE > OUTPUTFILE exit_code=$? if test $exit_code -eq 42 ; then # file processed OK, wasn't empty elif test $exit_code -eq 0 ; then # file was empty else # another sed error (e.g. bad program, I/O error, etc) fi As such, I'm marking this as "not a bug", but discussion can continue by replying to this thread. regards, - assaf From debbugs-submit-bounces@debbugs.gnu.org Mon Sep 20 15:40:29 2021 Received: (at 50701) by debbugs.gnu.org; 20 Sep 2021 19:40:29 +0000 Received: from localhost ([127.0.0.1]:44096 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1mSP9R-0003sC-8H for submit@debbugs.gnu.org; Mon, 20 Sep 2021 15:40:29 -0400 Received: from mout.gmx.net ([212.227.17.21]:33447) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1mSP9P-0003rw-H6 for 50701@debbugs.gnu.org; Mon, 20 Sep 2021 15:40:28 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.net; s=badeba3b8450; t=1632166821; bh=IaGyodtSg+RN/LvlHgbb/HMABTIRwpFldvZ7sM9vEEI=; h=X-UI-Sender-Class:Date:From:To:Subject:In-Reply-To:References; b=cbBUNInhkCDY+IfHnDv90FSuSlyrXJaeOVyhvgDv8bNcg0rkc22HhSshrnYVZKh3a +sjoPQT0SaFCBrT7iugT0VWWkZVN12LeXlsvmhoG5j4zei8O3QHHGooQS0xC+KR81C I0P9QWSkQ/WDJ7ZBFe7MPAOP9TOyOW6XZM5UsEuE= X-UI-Sender-Class: 01bb95c1-4bf8-414a-932a-4f6e2808ef9c Received: from gonzo ([79.12.50.90]) by mail.gmx.net (mrgmx105 [212.227.17.174]) with ESMTPSA (Nemesis) id 1MplXp-1nDpiS1K1t-00qCVR for <50701@debbugs.gnu.org>; Mon, 20 Sep 2021 21:40:21 +0200 Date: Mon, 20 Sep 2021 21:40:19 +0200 From: Davide Brini To: 50701@debbugs.gnu.org Subject: Re: bug#50701: cannot append or insert to empty file or stream Message-ID: <20210920214019.661725f9@gonzo> In-Reply-To: <20210920105348.1ae1bcc5@hale.su> References: <20210920105348.1ae1bcc5@hale.su> X-Mailer: Claws Mail 4.0.0 (GTK+ 3.24.30; x86_64-pc-linux-gnu) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable X-Provags-ID: V03:K1:2KawtTsWrpATyKN/99XykGd5a/Ecy87Gd1WG1a1gnNFwHaSphjP cthc2busA6qXTUbjjZJMXoLAXpyJbPmr3V5YAj+B9eL9mDAGnAWMK3a2TbVFyaQPz62nZ6a 51891cpZexQrJBOEjpbzcIAQ2JAx0+sE25q7N18mkUZeVZ4emtkZjV+MTw8PJwc2kvI+Dfz d5dIF6KwRB71C5FZoxU3w== X-Spam-Flag: NO X-UI-Out-Filterresults: notjunk:1;V03:K0:XwjcQWp+3i8=:fp7pfXufTd6XgNR9hrJFql l0n0NshVhGmGRi/gQ3iRTeuyTwYW0mZNs5TQHbEiVxWd0w5fw0Cl1yZBbWNipuD/viZljMDsl MWQYSwJ9fL1eEUo4DA8WwlNRjJ14wKAWAlqKcmCSGqaZ0ueUEFQwOLyF0958U72cSDV8KDvEv V4T7dsy/8LXDSk42fvtP0sn3FU5LCDPrCZg4QL9bsRaMKdED/R0PTQlFni8Ke7gshsYG+CBdO mDJNepbq269KCuRVkWZW6vTFIjBz4PjUfnpyxnUEjDYlz2omJRDTMgVaQyg/zenl4jbgaXrSq if0RQutsjsnPmHjsiRbaWmu3IT5xpi1pgz85QC6sQqPovZUp0U8MWKzxoH38y78nB0Kw/C0bQ fV3hoeOSXAIX9uO3SnSEutW+HyxR1azqSSrOHGIAPRcpReFjnMTvCGlxtvVknJL/JDnMTVJQw /vhoGqeUVWVJk6JYn+92UX7ii9+5eEC3y10g9p6sAb0maTJcKapdxJOc+dPKsp81IiS78VZWz K9tZrF+RI/+FnXsih4ep80zJXEzJI2PC1D0OUHqQUBNfPJFRNO0JE8iPux+VmpnW5zsaK8kEz /37K+WUjjWOHBTlATdcpHbHuBAyJ3wISUtSxfKKVJH3MCagWliQDHwU4AKi2jdcnw5Cvu3ojO BbMK/8P5Qno+3jZsgX8ebJB3ZDFfqVDMW2lWT0CQTjXQk4K9Tgrvk0RK4RXdyn0QpvtUPv/AO MLioHciBtoe98fGP8jSzK/2JUrwGtWnbFrK536iOHgF+5kltQ0Z1hC0vYvDhgHhjSTIdyD+J5 eIN1cPmY1ukslpkf+Kh6GM/CWE4LL64afBzhWmJpWbJP9zqWRTu7wGWTBcpdH6F1+WTki/RwK pHRxVgAy5nLRtvMCiqlrGpgo4Nphp3LT65/JfVnciAqgm/E0xSmUJ60N7qPGA6HJTecDGbvMV Qf4UTwKSvU6RixVJ0c1Wtg4RVFCc5BBqG8nOTw6CnMyoW4GgKho4JJOuag/lM477FJfsHPiv6 UBBnlASUc3XZGwRzpDyqEdIWQnYyoVe6QcVoFhzZ0ORKgayO7G2v988Is6I0YIzADfjl3s8H4 rlJ4GyTavuz1Ow= X-Spam-Score: 0.0 (/) X-Debbugs-Envelope-To: 50701 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 (-) On Mon, 20 Sep 2021 10:53:48 +0200, lexi hale wrote: > there is an inconsistency in the handling of the a and i commands in > sed. if the input stream or file immediately yields EOL, sed ignores > the commands and produces an empty stream. > > for instance: > > $ echo -n >file line-1 > $ sed -i aline-2 file > $ cat file > > correctly yields > > line-1 > line-2 > > however, > > $ touch empty > $ sed -i aline-1 empty > $ cat empty > > prints nothing, instead of the expected result "line-1". It's the same thing as the following: $ echo x > file $ sed -i 's|^|FOO|' file $ cat file FOOx $ touch empty $ sed -i 's|^|FOO|' empty $ cat empty $ Would you expect a line magically materializing in the seocnd case? I suppose you wouldn't... =2D- D. From debbugs-submit-bounces@debbugs.gnu.org Thu Sep 23 14:59:47 2021 Received: (at 50701) by debbugs.gnu.org; 23 Sep 2021 18:59:47 +0000 Received: from localhost ([127.0.0.1]:55895 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1mTTwg-0002RT-P6 for submit@debbugs.gnu.org; Thu, 23 Sep 2021 14:59:47 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:48499) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1mTTwe-0002RJ-TB for 50701@debbugs.gnu.org; Thu, 23 Sep 2021 14:59:46 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1632423584; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=B4PgfWwFcsN37vScPSWq8B22r4F844aKDpp+iPvtbeA=; b=ZolqKaeXc+Qok9cxVXk8qd7WHLeJwLYYcPC8ytrYgi0hcHoJjtwsvawdX7UV0z01qgArTf DapBj7rJGl+RRch2mZZn0p/SRGAyy7Uek5OUKWap+OuKF4KB920kUz0wnDT9nOuVbPhMRM l5jsteTfH5mUnUSzmgukASswo4eMz1s= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-335-vL7-a7PVMMukk-2fA6G4-g-1; Thu, 23 Sep 2021 14:59:39 -0400 X-MC-Unique: vL7-a7PVMMukk-2fA6G4-g-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id C26F484A5E3; Thu, 23 Sep 2021 18:59:38 +0000 (UTC) Received: from redhat.com (ovpn-115-8.phx2.redhat.com [10.3.115.8]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 3AC03608BA; Thu, 23 Sep 2021 18:59:38 +0000 (UTC) Date: Thu, 23 Sep 2021 13:59:36 -0500 From: Eric Blake To: Assaf Gordon Subject: Re: bug#50701: cannot append or insert to empty file or stream Message-ID: <20210923185936.p5xx6u7bms66irej@redhat.com> References: <20210920105348.1ae1bcc5@hale.su> <63a761b2-5366-ba7d-c51a-016c80cc5cf4@gmail.com> MIME-Version: 1.0 In-Reply-To: <63a761b2-5366-ba7d-c51a-016c80cc5cf4@gmail.com> User-Agent: NeoMutt/20210205-773-8890a5 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=eblake@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=us-ascii Content-Disposition: inline X-Spam-Score: -0.7 (/) X-Debbugs-Envelope-To: 50701 Cc: lexi hale , 50701@debbugs.gnu.org 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 (-) On Mon, Sep 20, 2021 at 01:28:42PM -0600, Assaf Gordon wrote: > First, let's clarify what the files are: > > > $ echo -n >file line-1 > > $ touch empty > > The file 'file' is not empty, it has one line. That's one way of viewing it. But according to POSIX, it doesn't even have that (the POSIX definition of a "line" is characters followed by a newline, and since you omitted the newline, it is not a line). > This line just happens to be empty (i.e. no characters in the line before > the newline character). > > The file 'empty' is empty, it contains NO lines. > ... > > > this is an extremely surprising behavior that limits the utility of sed > > when one cannot predict the contents of the file in question. > > I hope the explanation above makes this behavior less surprising. However, note that POSIX also says that 'sed' only has specified behavior for "text files". And the POSIX definition of a text file specifically excludes files that do not end in a newline, other than the empty file [1]. Thus, attempting to use sed on the file 'file' which does not end in a newline, and is therefore not a text file, is undefined behavior, and ANYTHING can happen. [1] https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap03.html#tag_03_403 (Other ways to have a file that is not a text file: have a NUL character, have more characters than LINE_MAX bytes between newlines, or have an encoding error in the current multibyte locale - but those are not relevant to this conversation.) But just because POSIX doesn't say what to do does not mean that we can't pick something useful. GNU sed tries hard to have sane behavior for "mostly-text" files, such as the case where you forgot a trailing newline. It does so by pretending that there was a newline after those last characters, after all. But not all sed implementations behave the same on your example. > > As such, I'm marking this as "not a bug", but discussion can continue > by replying to this thread. At any rate, I agree that it's not a bug. -- Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org From unknown Fri Aug 15 19:32:07 2025 Received: (at fakecontrol) by fakecontrolmessage; To: internal_control@debbugs.gnu.org From: Debbugs Internal Request Subject: Internal Control Message-Id: bug archived. Date: Fri, 22 Oct 2021 11:24:05 +0000 User-Agent: Fakemail v42.6.9 # This is a fake control message. # # The action: # bug archived. thanks # This fakemail brought to you by your local debbugs # administrator