I am very new to the shell scripting and I hope someone can help me with this.
I have thousands of files with certain format of information and I need to do this for all my files.
For each file, grab the numbers in the first and second rows and place them in the position 1 and 2 of the rest rows.
The numbers of the first and second rows are different for each file and also each file has different number of rows.
sed '
N
p
s/.* \(.*\)\n.* \(.*\)/\1 \2/
:loop
$d
N
s/\n/ /
p
s/\( [^ ]*\) .*/\1/
b loop
'
Narrative: When the first line is in the buffer, get the second, print both, extract the final fields of both and concatenate to be prefix, create a loop branch target, delete the prefix if there are no more lines, get the next line, turn the linefeed into a space, print it out, remove the suffix and loop.
All the codes suggested by ctsgnb are working flawlessly. Great work!
I tested the two codes by danmero and Franklin52 and nothing is changed in my file. I got no error but it just returned the original file back to me.
I have not tested the code by DGPickett yet. I am a newbie and I need more time to figure out what should I do with these lines! I will surely test it later today.
Just send your file through: sed '...' <input >output
Enjoy!
Awk should be better at this, as it has field sense. I just lay out my commands so they are easier to review. White space is almost free, but errors . . . .