Hi everyone,

Here is my problem:

I have different data files each with several columns and row of data.
datafile1 , datafile2, ...

Now I need to do some simple math procedure of this data files, Lets say I need to add the 3rd column of datafile1 to the 2nd column of the datafile2.

I was thinking to write a shell script that does so, but I am not sure where to start.
I think I need to first open up data files and then take different columns from each of them. I don't know what commands should I use or search for.

Any suggestion is very appreciated.

would think you first need to decide how you will pull out the columns from the files.
what is structure of your files, and what separates out the columns, if is simple separator (for example a | symbol) - then you might use something like:

awk -F \| '{print $2}' filename.txt

if you wanted to add up the column could do that in the same awk script.

adding entries in one column to entries in another column in another file - implies there is a relationship between rows in the file? if so, perhaps your problem might also be solved by pulling the files into tables in a db and being able to query them based on that relationship - then doing the math part would be relative simple.

else you might prefer to use awk to lookup rows in the secondary files while parsing the first file - this could involve using getline() function or other coprocess features that are well described in the manuals.

if you post more about how the files are related, about their structure, and an example of the math you need to perform then probably a more specific answer will be provided.


finally, is always good to remember that can feed math equations to 'bc' or 'dc' and even perform man equations using the 'expr' and possibly 'eval' functions in the shell itself.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.