If I have this...
6,
9,
12
"url": "https://www.url.com"
6,
9,
12
"url": "https://www.url.com"
13,
16
"url": "https://www.url.com"
"url": "https://www.url.com"
18
"url": "https://www.url.com"
"url": "https://www.url.com"
3,
6,
14
"url": "https://www.url.com"
"url": "https://www.url.com"
20
"url": "https://www.url.com"
74
"url": "https://www.url.com"
How can I join the lines in a way that gives me this...
6,9,12"url": "https://www.url.com"
6,9,12"url": "https://www.url.com"
13,16"url": "https://www.url.com"
"url": "https://www.url.com"
18"url": "https://www.url.com"
"url": "https://www.url.com"
3,6,14"url": "https://www.url.com"
"url": "https://www.url.com"
20"url": "https://www.url.com"
74"url": "https://www.url.com"
I have tried using sed to delete the newline on lines that start with a number, but it doesn't work. I think because the lines are changing as its working?
sed '/^[0-9]/N;s/\n//'
I get this...
6,9,
12"url": "https://www.url.com"
6,9,
12"url": "https://www.url.com"
13,16
"url": "https://www.url.com"
"url": "https://www.url.com"
18"url": "https://www.url.com"
"url": "https://www.url.com"
3,6,
14"url": "https://www.url.com"
"url": "https://www.url.com"
20"url": "https://www.url.com"
74"url": "https://www.url.com"
EDIT:
Thanks for the help and explanations.
I went with this one because it was easier for me to understand. They all worked though. sed ':a;/https/!{N;ba};s/\n//g'
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.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.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With