在图案处连接线。不均匀间隔

Chr*_*ris 6 awk sed

如果我有这个...

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"
Run Code Online (Sandbox Code Playgroud)

我怎样才能以一种给我这个的方式加入这些线条......

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"
Run Code Online (Sandbox Code Playgroud)

我曾尝试使用 sed 删除以数字开头的行上的换行符,但它不起作用。我想是因为线路在工作时发生了变化?

sed '/^[0-9]/N;s/\n//'
Run Code Online (Sandbox Code Playgroud)

我明白了这个...

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"
Run Code Online (Sandbox Code Playgroud)

编辑:感谢您的帮助和解释。我选择了这个,因为它对我来说更容易理解。他们都工作了。sed ':a;/https/!{N;ba};s/\n//g'

Enr*_*lis 5

以下代码应该可以工作:

sed ':a;/https/!{N;ba};s/\n//g'
Run Code Online (Sandbox Code Playgroud)

它实质上是一个while循环,它追加行后线,只要outcoming多不包含https; 一旦添加了包含 的一行https,就会放弃 while 循环(因为该b命令未执行),并且所有嵌入的换行符\n都将随该s命令一起删除。

更详细地说,脚本(在单引号之间)可以这样重写:

: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)
Run Code Online (Sandbox Code Playgroud)

对应的伪代码是

begin
while line does not contain "https" {
  append another line
}
remove all newlines
Run Code Online (Sandbox Code Playgroud)


Ste*_*eve 5

使用一种方法:

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