0

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.

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

Comments

2

you can try awk

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

2 Comments

I like the fact that we even chose the same variable name :)
Your solution worked flawlessly as well. Thanks a lot.
0
sed '#n
 /<name>/ {h
   b
   }
 G
 s/\n/ /p' YourFile

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.