From unknown Wed Jun 18 00:20:38 2025 X-Loop: help-debbugs@gnu.org Subject: bug#49873: Replacing all \n with spaces doesn't work in GNU sed as expected Resent-From: AlvinSeville7cf Original-Sender: "Debbugs-submit" Resent-CC: bug-sed@gnu.org Resent-Date: Wed, 04 Aug 2021 12:02:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 49873 X-GNU-PR-Package: sed X-GNU-PR-Keywords: To: 49873@debbugs.gnu.org X-Debbugs-Original-To: bug-sed@gnu.org Received: via spool by submit@debbugs.gnu.org id=B.16280784659614 (code B ref -1); Wed, 04 Aug 2021 12:02:02 +0000 Received: (at submit) by debbugs.gnu.org; 4 Aug 2021 12:01:05 +0000 Received: from localhost ([127.0.0.1]:42997 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1mBFa4-0002Uv-S6 for submit@debbugs.gnu.org; Wed, 04 Aug 2021 08:01:05 -0400 Received: from lists.gnu.org ([209.51.188.17]:45794) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1mBE7F-00046M-5g for submit@debbugs.gnu.org; Wed, 04 Aug 2021 06:27:13 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:36936) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mBE7E-0003cC-Tn for bug-sed@gnu.org; Wed, 04 Aug 2021 06:27:12 -0400 Received: from mail-lf1-x132.google.com ([2a00:1450:4864:20::132]:45855) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1mBE7D-0004lm-El for bug-sed@gnu.org; Wed, 04 Aug 2021 06:27:12 -0400 Received: by mail-lf1-x132.google.com with SMTP id g13so3552483lfj.12 for ; Wed, 04 Aug 2021 03:27:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:message-id:date:user-agent:mime-version :content-language; bh=NikwDyJ8WJ0ZotX1ZsZI3fYdWv53KB8VXRmEzeDzZC8=; b=SYyovbkRgbXM3LFB1TZkkkl/aof5eXbiIAEEg8w7r5ZIJLrxPPv+yckUhfZtFp3htv 93hNemfVFO+TMRoVFA01eFdYmwTuuemO0A9gEdLcQ2q2RVC0ciuVsZkLN4o/uuZDHJYX 21r5L2fQi6Hp3sKVxbWV8wxdBl730l9uV8oRPGGnBKFt0ijfwVNR6qqiCZd2e2fpSHTV +1sZoZDq7+/80Fsjs7j17Vs2wRCNkhLow2o0YPf0HzdkDzt+Qt2xdamRoN8AlRQi9hWB Dt8bNaD4BUnth8kYvEM7RvExPlKsQi1NDlj2JTq7SnR0SoZ2aNpJd63jcFEPTfXWHco+ vmqg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:message-id:date:user-agent :mime-version:content-language; bh=NikwDyJ8WJ0ZotX1ZsZI3fYdWv53KB8VXRmEzeDzZC8=; b=HQmY0E5f+PbI0deXxZww514mN8pzX8eLjHXAWDwRfV8Pt4tiajKaHVKPEXc01pznoE Kv6xfXyaFam1fiRjGW3VtezwxHu8L+y+T04QokIbYw16qc52hKXWadAVgYoOxu53pvuU qgqTqW/xCH+5VcCxNsWQi0sj8gOzNgiXQpZHNqciF82O80m+CeYiOLp/baUsA9B9r6Sa S8+PEJkNankchrSyuOF515lasgFBBisfjZVRL6AKLG2SfKxwCU2efb3Bs2jSOuoz3vzx 04YDTtqYYw+76Fi/Kc8/8WT5HEKqHv6GvuihrEv5ETLQzQd1j3WIbuAHqFWOXIy50bJo aXDw== X-Gm-Message-State: AOAM5328jKUSm3xnTY0F4Tf7pM2R4ZNOV8y2XG5Vbs1PORjQTzt8kgpS vCPqaSm4SaHMdZRIgj13YrPA54Rd4/0fjg== X-Google-Smtp-Source: ABdhPJxeoMJO6ibEjWmUBf4rbrp0cwArkdHH4Qesj1PMcL51mi9+Q0uuf8tip5FZlkcNBrC+OAVjzw== X-Received: by 2002:a05:6512:2035:: with SMTP id s21mr16270479lfs.451.1628072828982; Wed, 04 Aug 2021 03:27:08 -0700 (PDT) Received: from [192.168.0.148] ([109.126.36.21]) by smtp.gmail.com with ESMTPSA id p12sm166693lfr.300.2021.08.04.03.27.08 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 04 Aug 2021 03:27:08 -0700 (PDT) From: AlvinSeville7cf X-Google-Original-From: AlvinSeville7cf Message-ID: <8fa9f815-ae48-2ff7-f866-17374e67f740@gmail.com> Date: Wed, 4 Aug 2021 20:27:07 +1000 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.11.0 MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="------------361025872720114D26631348" Content-Language: en-US Received-SPF: pass client-ip=2a00:1450:4864:20::132; envelope-from=alvinseville7cf@gmail.com; helo=mail-lf1-x132.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, HTML_MESSAGE=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-Spam-Score: -1.3 (-) X-Mailman-Approved-At: Wed, 04 Aug 2021 08:01:03 -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 (--) This is a multi-part message in MIME format. --------------361025872720114D26631348 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Hello! I want to read entire file and then replace all *\n* with space. My sed script is (I know that it is not optimal but it demonstrates problem): |:a $! { N; ta } s/\n/ /g p | So why even with *g* flag *s* command replaces only first *\n* in pattern space? For instance I have the following file: |It was the best of times, it was the worst of times, it was the age of wisdom, it was the age of foolishness, | The result of script execution is: |It was the best of times, it was the worst of times, it was the age of wisdom, it was | I use GNU sed 4.8. It seems to be a bug. --------------361025872720114D26631348 Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: 7bit

Hello! I want to read entire file and then replace all \n with space. My sed script is (I know that it is not optimal but it demonstrates problem):

:a $! { N; ta }
s/\n/ /g
p

So why even with g flag s command replaces only first \n in pattern space? For instance I have the following file:

It was the best of times, it was
the worst of times, it
was the age of
wisdom,
it
was
the age
of foolishness,

The result of script execution is:

It was the best of times, it was the worst of times, it
was the age of wisdom,
it was

I use GNU sed 4.8. It seems to be a bug.

--------------361025872720114D26631348-- From unknown Wed Jun 18 00:20:38 2025 X-Loop: help-debbugs@gnu.org Subject: bug#49873: Replacing all \n with spaces doesn't work in GNU sed as expected Resent-From: Assaf Gordon Original-Sender: "Debbugs-submit" Resent-CC: bug-sed@gnu.org Resent-Date: Wed, 04 Aug 2021 20:08:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 49873 X-GNU-PR-Package: sed X-GNU-PR-Keywords: To: AlvinSeville7cf , 49873@debbugs.gnu.org Received: via spool by 49873-submit@debbugs.gnu.org id=B49873.162810765326933 (code B ref 49873); Wed, 04 Aug 2021 20:08:02 +0000 Received: (at 49873) by debbugs.gnu.org; 4 Aug 2021 20:07:33 +0000 Received: from localhost ([127.0.0.1]:45227 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1mBNAr-00070G-AJ for submit@debbugs.gnu.org; Wed, 04 Aug 2021 16:07:33 -0400 Received: from mail-pj1-f49.google.com ([209.85.216.49]:33738) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1mBNAp-0006zx-2P; Wed, 04 Aug 2021 16:07:31 -0400 Received: by mail-pj1-f49.google.com with SMTP id j18-20020a17090aeb12b029017737e6c349so6643370pjz.0; Wed, 04 Aug 2021 13:07:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-language:content-transfer-encoding; bh=tJYOobgGKLgdUZrW428vwTA45FB8jr1PmSGgUPFf4xY=; b=JRjDfm0iPzVXi85R7CbMXuN1az0vYx9cTN4LJYma0i05mRR/HA3ONq/WipJxAwkSK4 f7kV3740AYlpPbxORnALIV19sIjbt4sDFs2Twidzhggcab0NGh28e5IKlMNwRkDasDtR Pza5+NBzFX5glO3XT42uBbrud83vxSqjuPSyrcp/d7xlf2vM5jxH6yippiBaUiAWwKT4 evaU/ks52mQGRLE41lLPqocTxo8vDo2FYveeagNW4SOb/DOGcYtR89H6O8EstM8P5IxJ f2ZsNCOx/0q4DdHXJEV1suiJF2Rd1IDUNf+Gcv1fjG4S0KsDy/NUTtX7yhsammwqhkQX dOvQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; 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=tJYOobgGKLgdUZrW428vwTA45FB8jr1PmSGgUPFf4xY=; b=EgdrtIVU5LGLzNk8NBbqkegwJ/nWrdv5lekHEiNWRGyA9o2+Yx+gZT791Pi1ET0IXE 5aWvgG6FCaW4hl/WM4l9J5jG23E6HgrACDAe/EriezJDR8JYctdz44LRQaJRwxRvi/7b NhVvNB/Ix4fsBcDZ0YOZlxUEfBZwT7iQZmSbINVK3Hn5d6QjWesNO7tq4nB0PSYhCQum 0kAmFCrHdzejDir52WWKRecP5It3s9jnOwfmP+uHzEDE0/If8EefRMx1/AcQcKOHyKtv aqkDAe8PbYANBJP/CBDqALRNjoPD405+LbU7bZTY1t6CIliT1J+YdDEnVTKCuTzo1HDw IurQ== X-Gm-Message-State: AOAM533MEvAwXtWXTgNTw2cjmZxamnqenbby6s+58iNi/47qrJOdaUSJ eIkSs7oBVwqv+Ama2sC3l3mVFdR6ZG4= X-Google-Smtp-Source: ABdhPJxXl7Pg7fgQNY0AJJIwB2xvwjWIaJ0nIcqeYdk6vtmaS5bz7pwBuKWbNxlpH0EWiS6ZixBmEQ== X-Received: by 2002:a17:90a:420c:: with SMTP id o12mr812202pjg.101.1628107644738; Wed, 04 Aug 2021 13:07:24 -0700 (PDT) Received: from tomato.moose.housegordon.com (moose.housegordon.com. [184.68.105.38]) by smtp.googlemail.com with ESMTPSA id q5sm3371228pjo.7.2021.08.04.13.07.23 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 04 Aug 2021 13:07:23 -0700 (PDT) References: <8fa9f815-ae48-2ff7-f866-17374e67f740@gmail.com> From: Assaf Gordon Message-ID: Date: Wed, 4 Aug 2021 14:07:22 -0600 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.10.0 MIME-Version: 1.0 In-Reply-To: <8fa9f815-ae48-2ff7-f866-17374e67f740@gmail.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit X-Spam-Score: -0.1 (/) 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 49873 notabug close 49873 stop Hello, On 2021-08-04 4:27 a.m., AlvinSeville7cf wrote: > Hello! I want to read entire file and then replace all *\n* with space. For that I would recommend using 'tr' - it'll be much faster: tr '\n' ' ' < input > output > My sed script is (I know that it is not optimal but it demonstrates > problem): > > |:a $! { N; ta } s/\n/ /g p | The above script isn't valid as-is (perhaps line breaks were lost in the email?). I'm going to assume you meant the following script, and used "sed -n": sed -n ':a $! { N; ta } ; s/\n/ /g ; p' < input > output or with line breaks: sed -n ':a $! { N; ta } s/\n/ /g p' < input > output > So why even with *g* flag *s* command replaces only first *\n* in > pattern space? For instance I have the following file: You script is almost correct :) I assume that with the "$!{N;ta}" command you meant to accumulate all lines except the last in the pattern space, and then replace all the new lines and print the patern space. The only 'bug': "t" is "conditional jump". It jumps once to label "a", accumulating one more line, but then doesn't jump again - so the "s///" is executed and the two lines are printed (and one newline replaced with space). The "s///" command also resets the "t" conditional, so the next line (3rd line in the input file) then does causes a jump. Observe: $ seq 10 | sed -n ':a $! { N; ta } ; s/\n/ /g ; p' 1 2 3 4 5 6 7 8 9 10 If you replace the "t" with a "b" command (b = always jump), it behaves as you expected: $ seq 10 | sed -n ':a $! { N; ba } ; s/\n/ /g ; p' 1 2 3 4 5 6 7 8 9 10 Note that even with this script, the last newline is preserved and printed. As a work-around, you can instruct "sed" to use NUL as line-breaks, causing "\n" characters to be treated like any other character: $ seq 10 | sed -z 's/\n/ /g' 1 2 3 4 5 6 7 8 9 10 But this won't be as efficient as using 'tr'. > |It was the best of times, it was the worst of times, it was the age of > wisdom, it was the age of foolishness, | > > The result of script execution is: > > |It was the best of times, it was the worst of times, it was the age of > wisdom, it was | > I use GNU sed 4.8. It seems to be a bug. Without line breaks it's a bit hard to reproduce your case, but I hope the explanation above was sufficient. As such I'm closing this as "not a bug", but discussion can continue by replying to this thread. regards, - assaf