战略经济对话。如何将前一个字符串附加到当前字符串?
SED. How to append a previous string to a current?
我有一个包含以下字符串的文件:
1.44 1.12
disk 1.00 0.15 0.21
1.15 1.08
disk2 0.15 0.13 0.11
如何将 1 个字符串附加到 2。不是 2 到 1。要像这样:
disk 1.00 0.15 0.21 1.44 1.12
disk2 0.15 0.13 0.11 1.15 1.08
例如 sed
?
如果你想连接两个连续的行,你可以在awk
中这样说:
$ awk 'NR%2 {prev=[=10=]; next} {print [=10=], prev}' file
disk 1.00 0.15 0.21 1.44 1.12
disk2 0.15 0.13 0.11 1.15 1.08
这会将奇数行存储在变量 prev
中,稍后将其与偶数行一起打印。
使用 sed:
sed -n 'h;n;G;s/\n/ /;p' file
这将
h # save the line in the hold buffer
n # fetch the next line to the pattern space
G # append the hold buffer to the pattern space
s/\n/ / # replace the newline between them with a space
p # and print the result.
这里是另一个 awk
版本,如果所有需要的行都以 data
:
开头
awk '/^disk/ {print [=10=],p} {p=[=10=]}' file
disk 1.00 0.15 0.21 1.44 1.12
disk2 0.15 0.13 0.11 1.15 1.08
一些打高尔夫球:
awk '/^disk/&&[=11=]=[=11=]p; {p=[=11=]}' file
disk 1.00 0.15 0.211.44 1.12
disk2 0.15 0.13 0.111.15 1.08
我有一个包含以下字符串的文件:
1.44 1.12
disk 1.00 0.15 0.21
1.15 1.08
disk2 0.15 0.13 0.11
如何将 1 个字符串附加到 2。不是 2 到 1。要像这样:
disk 1.00 0.15 0.21 1.44 1.12
disk2 0.15 0.13 0.11 1.15 1.08
例如 sed
?
如果你想连接两个连续的行,你可以在awk
中这样说:
$ awk 'NR%2 {prev=[=10=]; next} {print [=10=], prev}' file
disk 1.00 0.15 0.21 1.44 1.12
disk2 0.15 0.13 0.11 1.15 1.08
这会将奇数行存储在变量 prev
中,稍后将其与偶数行一起打印。
使用 sed:
sed -n 'h;n;G;s/\n/ /;p' file
这将
h # save the line in the hold buffer
n # fetch the next line to the pattern space
G # append the hold buffer to the pattern space
s/\n/ / # replace the newline between them with a space
p # and print the result.
这里是另一个 awk
版本,如果所有需要的行都以 data
:
awk '/^disk/ {print [=10=],p} {p=[=10=]}' file
disk 1.00 0.15 0.21 1.44 1.12
disk2 0.15 0.13 0.11 1.15 1.08
一些打高尔夫球:
awk '/^disk/&&[=11=]=[=11=]p; {p=[=11=]}' file
disk 1.00 0.15 0.211.44 1.12
disk2 0.15 0.13 0.111.15 1.08