help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Concatenate files by date


From: Olaf Till
Subject: Re: Concatenate files by date
Date: Tue, 14 Jun 2016 18:06:16 +0200
User-agent: Mutt/1.5.23 (2014-03-12)

On Tue, Jun 14, 2016 at 06:20:20AM -0700, babelproofreader wrote:
> I have separate text files that consist of a date column followed by columns
> of relevant data and I would like to load them and concatenate them into one
> file that has the date column and then all the relevant data from the
> following columns from the separate files. The date columns will
> occasionally have missing/different dates so I'd like the date columns to be
> merged whilst preserving the correct date stamps for the rows/columns of the
> concatenated data.
> 
> A simple example:
> 
> File A
> 14/06/2016, 1, 2, 3
> 16/06/2016, 4, 5, 6
> 
> File B
> 15/06/2016, 7, 8, 9
> 17/06/2016, 10, 11, 12
> 
> should be merged to
> 
> File C
> 14/06/2016, 1, 2, 3
> 15/06/2016,  ,  ,  , 7, 8, 9
> 16/06/2016,4, 5, 6,  ,  ,  
> 17/06/2016,  ,  ,  , 10, 11, 12 
> 
> Can this be done in Octave, and if so, what is the Octave syntax?

It can be done, in several ways, and it's rather a matter of code than
of syntax -- you probably won't find a function dedicated to
completely solving this special problem.

In the 'io' package, there is a function 'csv2cell'. You could use it
to read in the files into cell arrays:

a = csv2cell ("filea");
b = csv2cell ("fileb");

Your example has 7 columns in the merged file, add an extra coulumn to
fill it with a sortable date later:

new_a = cell (rows (a), 8);
new_b = cell (rows (b), 8);

new_a(:, [2:5]) = a;
new_b(:, [2, 6:8]) = b

concatenate new_a and new_b:

ab = vertcat (new_a, new_b);

Fill in the extra column with a sortable date:

for id = 1 : rows (ab)
  ab{id, 1} = mktime (strptime (ab{id, 2}, "%d/%m/%Y")); # or %e
                                       # instead of %d, see strptime help
endfor

Then use 'sort' to get a sort index for the first column, delete the
first column, and apply the sort index to the rows of 'ab'. You still
have to write the result out to a file with a suitable function, there
is probably one.

If the source files are already sorted, you alternatively could
'fopen' them both at the same time, concurrently read them in line by
line, compare the dates in the lines and write out the merged file on
the fly.

Maybe others like to give you a complete cooking recipe. It might help
to explain what you meant with 'missing or different' dates.

Olaf

-- 
public key id EAFE0591, e.g. on x-hkp://pool.sks-keyservers.net

Attachment: signature.asc
Description: Digital signature


reply via email to

[Prev in Thread] Current Thread [Next in Thread]