Comments on: How to match newlines in sed https://backreference.org/2009/12/23/how-to-match-newlines-in-sed/ Proudly uncool and out of fashion Mon, 11 Feb 2019 16:24:29 +0000 hourly 1 https://wordpress.org/?v=5.8.2 By: waldner https://backreference.org/2009/12/23/how-to-match-newlines-in-sed/#comment-25471 Mon, 11 Feb 2019 16:24:29 +0000 http://backreference.org/?p=96#comment-25471 In reply to Robin.

The first version has to repeatedly scan (once per input line) an ever-increasing pattern space. the s/\n/ / takes longer and longer as input lines accumulate.
The second version slurps the whole file first, and then runs a *single* s/\n/ /g command, which goes over the whole file just once.

]]>
By: Robin https://backreference.org/2009/12/23/how-to-match-newlines-in-sed/#comment-25469 Fri, 08 Feb 2019 06:30:31 +0000 http://backreference.org/?p=96#comment-25469 Why is it so slow:

cd "$(mktemp -d)"
tr -dc '[:print:]' file.txt
time sed ':begin;$!N;s/\n/ /;tbegin' processed.txt

# real 2m29.278s
# user 2m28.486s
# sys 0m0.022s

and this (https://stackoverflow.com/a/1252191) so fast:

time sed ':begin;N;$!bbegin;s/\n/ /g' | processed.txt

# real 0m0.026s
# user 0m0.013s
# sys 0m0.013s

]]>
By: Ingmar Boddington https://backreference.org/2009/12/23/how-to-match-newlines-in-sed/#comment-24900 Thu, 23 May 2013 22:43:02 +0000 http://backreference.org/?p=96#comment-24900 In reply to waldner.

...good explanation :) I understand what you are saying ofc, but I'd used sed for replacements many times before having to use labels whereas N and P had been used before. Thanks for your responses.

]]>
By: waldner https://backreference.org/2009/12/23/how-to-match-newlines-in-sed/#comment-24899 Thu, 23 May 2013 16:34:34 +0000 http://backreference.org/?p=96#comment-24899 In reply to Ingmar Boddington.

Well, labels and branches are (or should be) basic sed knowledge.
":begin" defines a label, and "t" is the conditional branch command, so "tbegin" (or "t begin") branches to label ":begin" if the last replacement operation was successful. Think of it as a kind of conditional "goto".
The less known commands "N", "P" and "D" instead deserve more explanation in my opinion, since here they are integral to newline matching (while labels and branches are only used as part of the program and are not essential to understand how newlines are actually matched).

]]>
By: Ingmar Boddington https://backreference.org/2009/12/23/how-to-match-newlines-in-sed/#comment-24898 Thu, 23 May 2013 16:25:21 +0000 http://backreference.org/?p=96#comment-24898 In reply to waldner.

Hi, that is (kind of) true - it is after all an article called 'HOW TO match newlines in sed'. Simply explaining your examples more fully would have greatly increased the value of this post. Thanks anyway.

]]>