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

标签 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"

我如何才能以这种方式加入队伍...
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"

我尝试使用sed删除以数字开头的行上的换行符,但是它不起作用。我认为是因为线路随着工作而改变了?
sed '/^[0-9]/N;s/\n//'

我明白了...
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"

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

最佳答案

下面的代码应该工作:

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

本质上是一个while循环,只要输出的多行确实而不是包含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)

相应的伪代码为
begin
while line does not contain "https" {
  append another line
}
remove all newlines

关于awk - 在图案处连接线。不均匀的间隔,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60857995/

相关文章:

awk - 使用 awk 将多个字符替换为 , csv 的逗号

json - 在没有尾随分隔符的情况下分隔 AWK 中的输出记录

bash - 对 docker 容器执行 sed 命令

git - 使用 awk sed 解析更新 puppet 文件

regex - grep 匹配整个证书?

awk - 我想将 file1 列(1,2,4,5)映射到 file2 列(1,2,4,5)。第 5 列可能包含不同顺序的逗号分隔字符 (A,T,G,C)

linux - 得到没有。为某个日期范围打印的行数

linux - 使用环境变量(从 awk 命令获取其值)作为 while 中的条件

linux - Bash:无法使用计算识别具有范围的文件名

linux - 请帮助从文本中删除特定行