Join lines at pattern. Uneven interval

The following code should work:

sed ':a;/https/!{N;ba};s/\n//g'

It is essentially a while loop, which appends line after line, as long as the outcoming multiline does not contain https; as soon as one line is appended which contains https, the while loop is abandoned (as the b command is not executed), and all embedded newlines \n are removed with the s command.

More in detail, the script (between single quotes) can be rewritten like this:

:a        # label you can jump to with a t or b command
/https/!{ # if the line does not match "https" do what's in {…}:
    N     #   append the next line to the current one (putting "\n" in between)
    ba    #   branch to the line labelled as ":a"
}
s/\n//g   # change all newlines to empty strings (i.e. remove all newlines for the current multi-line)

The corresponding pseudo-code would be

begin
while line does not contain "https" {
  append another line
}
remove all newlines

One way using awk:

awk '{ printf("%s%s", $0, /^[0-9]/ ? "" : "\n") }' file.txt

sed '/^[0-9]/{H;d};H;s/.*//;x;s/\n//g'
  • /^[0-9]/ - If the line starts with a digit.
    • H - Append the line to hold space.
    • d - Delete the line and start over.
  • If the line does not start with a digit
  • H - Append the line to hold space to all the digits there.
  • s/.*// - Clear pattern space. I want to clear hold space.
  • x - Switch pattern space with hold space.
  • s/\n//g - Replace all the newlines by nothing.
  • And here the line with the numbers are printed.

Tags:

Awk

Sed