There are many ways to do this. This is a simple first pass:
text = 'ANALYSIS OF X SIGNAL, CASE: 1 TUNE X = 0.2561890123390808 Line Frequency Amplitude Phase Error mx my ms p 1 0.2561890123391E+00 0.204316425208E-01 0.164145385871E+03 0.00000000000E+00 1 0 0 0 2 0.2562865535359E+00 0.288712798671E-01 -.161563284233E+03 0.97541196785E-04 1 0 0 0 ANALYSIS OF X SIGNAL, CASE: 1 TUNE X = 1.2561890123390808 Line Frequency Amplitude Phase Error mx my ms p 1 1.2561890123391E+00 0.204316425208E-01 0.164145385871E+03 0.00000000000E+00 1 0 0 0 2 1.2562865535359E+00 0.288712798671E-01 -.161563284233E+03 0.97541196785E-04 1 0 0 0 ANALYSIS OF X SIGNAL, CASE: 1 TUNE X = 2.2561890123390808 Line Frequency Amplitude Phase Error mx my ms p 1 2.2561890123391E+00 0.204316425208E-01 0.164145385871E+03 0.00000000000E+00 1 0 0 0 2 2.2562865535359E+00 0.288712798671E-01 -.161563284233E+03 0.97541196785E-04 1 0 0 0 ' require 'stringio' pretend_file = StringIO.new(text, 'r')
This gives us a StringIO object that we can pretend to be a file. We can read from it line by line.
I changed the numbers a bit to make it easier to see how they are written in the output.
pretend_file.each_line do |li| case when li =~ /^TUNE.+?=\s+(.+)/ print $1.strip, "\n" when li =~ /^\d+\s+(\S+)\s+(\S+)/ print $1, ' ', $2, "\n" end end
For real use, you want to change the print statements to a file descriptor: fileh.print
The result is as follows:
# >> 0.2561890123390808 # >> 0.2561890123391E+00 0.204316425208E-01 # >> 0.2562865535359E+00 0.288712798671E-01 # >> 1.2561890123390808 # >> 1.2561890123391E+00 0.204316425208E-01 # >> 1.2562865535359E+00 0.288712798671E-01 # >> 2.2561890123390808 # >> 2.2561890123391E+00 0.204316425208E-01 # >> 2.2562865535359E+00 0.288712798671E-01
the tin man
source share