Friday, June 11, 2010

[QnA] Command line rocks!! Combine several data files...

I have several data files. Each of them has only one column which presents acceleration along one axis. I want to combine them into one single file so that I can read them into my Python program with opening only one file.

To illustrate the situation, here are some sample contents of the files:

ACC_X.txt
126
127
129
127
137

ACC_Y.txt
132
106
109
114
105

ACC_Z.txt
137
139
138
138
144

What I want is to combine them in columns within a single files as:
ACC.txt
126,132,137
127,106,139
129,109,138
127,114,138
137,105,144

The first thing came into my head is using awk. However, I am not familiar with awk script. With brief searching, I found two easy ways in the command line: paste and pr.

The command using paste could be:
$ paste -d, ACC_X.txt ACC_Y.txt ACC_Z.txt > ACC.txt

Or you can use pr like this:
$ pr -mts, ACC_X.txt ACC_Y.txt ACC_Z.txt > ACC.txt

The comma in the commands means I am using it as the delimiter or separator.

Simple and fast. Command line really rocks!

No comments:

Post a Comment