You are not logged in.

#1 2009-05-13 22:44:05

jnwebb
Member
Registered: 2008-07-07
Posts: 52

quick file formatting help

I have a text file that is formatted as follows:

-0.0016175000
-0.0017733000
-0.0018856000
-0.0019120000
-0.0018445000
-0.0016527000
-0.0013274000
-0.0009151100
-0.0004094200
-0.0001299200
0.0000949790
0.0000043996
-0.0002285200
-0.0002984000
-0.0004567800
-0.0007942500
.....etc to 4096 lines

I would like to format it to 8f9.6 (8 columns, 9 characters per column, 6 decimal places) so that it looks like this : (ignore the numbers)

 0.000798 0.000767 0.000724 0.000734 0.000782 0.000795 0.000826 0.000902
 0.000979 0.000967 0.000965 0.001138 0.001277 0.001257 0.001289 0.001465
 0.001700 0.001913 0.002056 0.002195 0.001990 0.001317 0.000547-0.000331
-0.001186-0.001798-0.002032-0.002070-0.002350-0.002688-0.003055-0.003558
-0.003862-0.003885-0.003795-0.004288-0.005034-0.004921-0.004742-0.004633
-0.004324-0.003811-0.003159-0.003647-0.004038-0.003878-0.004468-0.004487
-0.004155-0.003389-0.002787-0.004875-0.006885-0.008231-0.010034-0.010298
-0.010232-0.009111-0.007179-0.005626-0.002845-0.001475-0.000203 0.003708
 0.006822 0.009041 0.009653 0.008051 0.006684 0.003679 0.001132 0.000948
 0.000256 0.000046 0.000609 0.002814 0.005005 0.004153 0.004023 0.005317
...etc.

Thank you

Offline

#2 2009-05-13 23:27:53

lucke
Member
From: Poland
Registered: 2004-11-30
Posts: 4,018

Re: quick file formatting help

Not a beauty, but should work (reads from "file", outputs to standard output).

#! /usr/bin/ruby -w

arr = []
x = 0

File.open("file", "r") do |file|
  while line = file.gets
    arr.insert(-1,line) 
  end
end

arr.each do |element|
  print sprintf("%9.6f",element)
  x = x + 1
  puts if x.modulo(8) == 0
end

Last edited by lucke (2009-05-13 23:38:11)

Offline

#3 2009-05-13 23:47:22

jnwebb
Member
Registered: 2008-07-07
Posts: 52

Re: quick file formatting help

I am really new to this:  so w/ the above set of commands, I just input as shown into the command line, except put the filename where you have "file"?

Thanks

Offline

#4 2009-05-13 23:50:19

jnwebb
Member
Registered: 2008-07-07
Posts: 52

Re: quick file formatting help

I am using bash by the way

Offline

#5 2009-05-13 23:51:41

lucke
Member
From: Poland
Registered: 2004-11-30
Posts: 4,018

Re: quick file formatting help

Copy the code to a file, save this file as program.rb, run "chmod +x program.rb" in console, then "./program.rb". The source file has to be in the same directory and has to be called "file", otherwise change "file" to your filename in the File.open().

And you need to have ruby installed (pacman -S ruby).

Last edited by lucke (2009-05-13 23:53:21)

Offline

#6 2009-05-14 13:03:24

jnwebb
Member
Registered: 2008-07-07
Posts: 52

Re: quick file formatting help

Thank you very much, that worked perfectly!  I appreciate the help.

Offline

Board footer

Powered by FluxBB