-1

I have a file with 48 lines like

0.00000
0.00621
0.16298
0.19977
0.40198
0.29450
0.49277
0.28054
0.43256
0.18729
0.28934

And I want to prepend d.ddddddd, to each line, each d represents a digit (so the calculation should be rounded).

The number prepended is calculated as n/48000 where n is the greatest integer less than the line number (0-indexed, the first line should have 0 prepended)

It should end up looking like

0.0000000,0.00000
0.0000208,0.00621
0.0000417,0.16298
0.0000625,0.19977
0.0000833,0.40198
0.0001042,0.29450

and so on.

  • 1
    Maybe you should show us what you tried before so that people don't have to do all of the job but simply help you with the substitution which causes you a problem. Also, I'm not sure vim is really the best tool for what you are trying to do: maybe awk or a simple python script would be a better fit. – statox Oct 9 '17 at 8:43
2

Not sure, I understand exactly want you are trying to achieve. But given your test data, I came up with the following substitution:

:%s#.*#\=printf("%.07f,%s", ((line('.')-1)/48000.0), submatch(0))

Which uses the substitution command and prepends the result of (dividing line number -1, (line('.')-1)) by 48000 to each line and formats it to 7 digits.

This needs a vim with +float feature.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.