In Linux, I have a program that changes a list of coordinates slightly as it runs through some number of iterations. I can use grep -c to count the number of iterations and I want to grab the last set of labels and coordinates from that programs output.
My source file looks something like:
text
total number of objects with coordinates = n
text
begin coordinates
label1 x1 y1 z1
label2 x2 y2 z2
...
labeln xn yn zn
end coordinates
text
text
begin coordinates
label1 a1 b1 c1
label2 a2 b2 c2
...
labeln an bn cn
end coordinates
text
text
In this case I would want to dump the set of labels along with their a, b and c coordinates to a text file that would look like this:
label1 a1 b1 c1
label2 a2 b2 c2
...
labeln an bn cn
My preference would be to use something like grep or awk to do this, but I’m not familiar enough with them to implement this.
EDIT: I forgot to mention that the labels are neither constant nor unique so label2 could equal label5 or labeln.
Whenever you see
begin coordinates, start saving a new block of text. Stop saving it when you seeend coordinates. At the end of the file, print the last block of text that you saved.Note that
text = text $0 "\n"is concatenating the variablestextand$0with\n. Whitespace is awk’s concatenation operator.