PureBytes Links
Trading Reference Links
|
Needing to modify something in multiple ASCII files can be a
frustrating, time consuming experience with a mouse driven editor
or application. However, the operations can be automated with a
script driven editor such as Vim or Gvim so that all the
operations to the file are completed with a single command, such
as,
Gvim quotefile.txt -s space2comma.vim
Vim or Gvim is available at
http://www.vim.org/
There are lots of ways of modifying text files, but after gaining
some experience with Gvim, one can easily experiment and see what
is happening. Then after the process to create the desired result
has been determined the operations can be automated so that it
can be quickly applied to many files.
Learning some of the capabilities of Vim can greatly improve ones
productivity in manipulating the data in ASCII files. However,
the use of Vim is not intuitively obvious and most people will
need to read some of the documentation about using it. Otherwise,
all of us may hear their screams of frustration.
Now that you are warned, a procedure for using Gvim for
substituting commas for the spaces and adding a header to a quote
file will be shown.
>From Ken's request
>Here's a sample of what I am working with.
>
>19971201 925 118.71875 118.71875 118.71875 118.71875 2
>19971201 951 118.71875 118.71875 118.71875 118.71875 1
>19971201 954 118.78125 118.78125 118.78125 118.78125 1
>
Assuming the desired result is the following format which can be
converted with the MetaStock Downloader:
<date>,<time>,<open>,<high>,<low>,<close>,<vol>
19971201,925,118.71875,118.71875,118.71875,118.71875,2
19971201,951,118.71875,118.71875,118.71875,118.71875,1
19971201,954,118.78125,118.78125,118.78125,118.78125,1
Changing the spaces to commas in a large number of files is
fairly easy if using Gvim. It is just necessary to specify the
operations for the editor to perform in a script file and then
have the editor perform those operations on each text file.
Though the Gvim commands are cryptic, the automation concept is
straight forward. After selecting a file to be fixed, the
actions to automate are:
1. Substitute a single comma for each sequence of spaces
in all lines. (:1,$ s/ */,/g)
2. Insert the header line before the first line. (:0r header.txt)
3. Write the modified file and quit Gvim (:wq)
If the two required files have been created and are in the same
directory as the quote file, these operations can be performed by
executing, from a MSDOS window, the single command:
Gvim quotefile.txt -s space2comma.vim
1. The first required file, named header.txt in this example,
contains the header in the single line:
<date>,<time>,<open>,<high>,<low>,<close>,<vol>
2. The second required file is the Vim script file, named
space2comma.vim in this example, which contains the Gvim
commands. It is exactly the three following lines beginning with
":" and including the ":"
:1,$ s/ */,/g
:0r header.txt
:wq
Since this script writes the changes to the file, the original
files will be replaced with the changed data, so please BACKUP
YOUR FILES just in case something goes wrong!
If you work with ASCII files frequently, you may find it
worthwhile to learn how to perform those specific tasks with Vim
or Gvim.
Doug Fogg
>
>Date: Tue, 19 Mar 2002 18:45:30 -0500
>From: "iamken@xxxxxxxxxxxxxxx" <iamken@xxxxxxxxxxxxxxx>
>Subject: converting non comma separated ASCII data into MSWIN format
>
>Hi,
>
>I have a boatload of non comma separated ASCII data that I need to
>convert into Metastock format, but I don't know how to get commas
>between the fields. My feeble attempts to do this in Excel have failed,
>but I think there must be a painless way to do this? Any help would be
>appreciated.
>
>Here's a sample of what I am working with.
>
>19971201 925 118.71875 118.71875 118.71875 118.71875 2
>19971201 951 118.71875 118.71875 118.71875 118.71875 1
>19971201 954 118.78125 118.78125 118.78125 118.78125 1
>
>Thanks!
>Ken
>
>------------------------------
|