我有一个包含值的文本文件,我正在尝试创建并填充文件中特定行的列。更确切地说,输入如下:
<name>RELATIVE 1</name>
0.624790,-0.000000,0
0.493925,0.591035,0
0.363039,1.182079,0
<name>RELATIVE 2</name>
-24.621334,0.000000,0
-24.752199,0.591031,0
-24.883085,1.182072,0
<name>RELATIVE 3</name>
-49.868542,0.000000,0
-49.999397,0.591014,0
-50.130271,1.182033,0
,预期输出应为:
0.624790,-0.000000,0 <name>RELATIVE 1</name>
0.493925,0.591035,0 <name>RELATIVE 1</name>
0.363039,1.182079,0 <name>RELATIVE 1</name>
-24.621334,0.000000,0 <name>RELATIVE 2</name>
-24.752199,0.591031,0 <name>RELATIVE 2</name>
-24.883085,1.182072,0 <name>RELATIVE 2</name>
-49.868542,0.000000,0 <name>RELATIVE 3</name>
-49.999397,0.591014,0 <name>RELATIVE 3</name>
-50.130271,1.182033,0 <name>RELATIVE 3</name>
解决方案可以是Bash,Awk或Sed。
提前感谢您的帮助。
答案 0 :(得分:2)
使用awk:
awk '/^</{s=$0;next}{print $0,s}' file.txt
当一行以<
开头时,将变量s
设置为该行的内容并跳至下一行。在其他行上,打印行的内容,然后打印s
。
答案 1 :(得分:2)
您可以尝试awk
awk '/name/{s=$0}!/name/{print $0,s}' file
答案 2 :(得分:0)
sed '#n
/<name>/ {h
b
}
G
s/\n/ /p' YourFile
使用保留缓冲区
将最新的 name 行添加到当前行