npires
npires

Reputation: 6693

Linux create columns from row

I have a text file with values, and I'm trying to create and fill a column from a specific row in the file. To be more precise, the input is the following:

<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

and the expected output should be:

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>

the solution could be in Bash, Awk or Sed.

Thanks in advance for the help.

Upvotes: 0

Views: 706

Answers (3)

NeronLeVelu
NeronLeVelu

Reputation: 10039

sed '#n
 /<name>/ {h
   b
   }
 G
 s/\n/ /p' YourFile

add the latest name line to the current line using the holding buffer

Upvotes: 0

ghostdog74
ghostdog74

Reputation: 342363

you can try awk

awk '/name/{s=$0}!/name/{print $0,s}' file

Upvotes: 2

Tom Fenech
Tom Fenech

Reputation: 74615

Using awk:

awk '/^</{s=$0;next}{print $0,s}' file.txt

When a line begins with <, set the variable s to the contents of the line and skip to the next line. On other lines, print the contents of the line, followed by the contents of s.

Upvotes: 2

Related Questions