My data is formatted like the following.
Each line that documents a source/shot:
s 0.01 0 0 -1 0
Every other line that follows the source/shot information:
r 0.1 0 0 1.218 0.01
r 0.15 0 0 1.214 0.01
r 0.2 0 0 1.213 0.01
I can use the "s" as a flag for the shot lines, and I would like to count the number of "r" lines for each source/shot and insert that number into the corresponding "s" line.
The number of "r" lines for each "s" line varies greatly.
Given this sample input:
s 0.01 0 0 -1 0
r 0.1 0 0 1.218 0.01
r 0.15 0 0 1.214 0.01
r 0.2 0 0 1.213 0.01
s 1.01 0 0 -1 0
r 0.05 0 0 1.159 0.01
r 0.1 0 0 1.127 0.01
r 0.15 0 0 1.106 0.01
r 0.2 0 0 1.115 0.01
r 0.25 0 0 1.107 0.01
The expected output is:
s 0.01 0 3 -1 0
r 0.1 0 0 1.218 0.01
r 0.15 0 0 1.214 0.01
r 0.2 0 0 1.213 0.01
s 1.01 0 5 -1 0
r 0.05 0 0 1.159 0.01
r 0.1 0 0 1.127 0.01
r 0.15 0 0 1.106 0.01
r 0.2 0 0 1.115 0.01
r 0.25 0 0 1.107 0.01
Note the 3 as $4 in the first s line and the 5 as $4 in the second one.
The counted number of rows should be in the 4th column of each "s" line (asterisks here).
My experience with awk is limited to just rearranging/indexing columns, so I don't really know where to begin with this. I've tried googling help with awk, but it's very difficult to find answered awk questions that actually pertain to my specific situation (hence why I have decided to ask it myself).
I'm also new to using stackoverflow, so if I need to include more example data, please let me know. My data consists of approximately 4000 lines.
EDIT: The reason the desired result has slightly different data to the example of my data is because there are hundreds of lines for each "s" line and including that in the question seems excessive. I have cut out the majority of the data for ease of reading.
0 comments:
Post a Comment
Thanks