Find the answer to your Linux question:
Results 1 to 3 of 3
Hi All, I have a file with hundreds of lines and many columns and I need to process the numbers on each line in special way, basically adding and multiplying. ...
  1. #1
    Just Joined!
    Join Date
    Mar 2011
    Posts
    5

    adding numbers from different columns in one file

    Hi All,
    I have a file with hundreds of lines and many columns and I need to process the numbers on each line in special way, basically adding and multiplying. My problem is that I don't know how to do it because the numbers are delimited in special way and I don't know how to read the correct numbers and then process them. Furthermore the numbers are in scientific form "1.112224e+00".

    Each line is structured as following.

    tab number space number space number tab tab number ..... number enter


    here is also simple short example of one line


    1.112224e+00 1.127221e+00 1.136870e+00 2.693811e+01 2.721771e+01 2.784709e+01 1.112224e+00 1.127221e+00 1.136870e+00 2.693811e+01 2.721771e+01 2.784709e+01 3.693811e+01 2.721771e+01 2.784709e+01


    What I have to do is to add together number on position 1+4+7.... number, 2+5+8 ...number, 3+6+9 ....number, etc . The results should be three columns. Then I have to multiply each number from those three by coefficient and then add those three numbers together.

    I forgot to mention that I want to do it in bash script.

    Thank you very much for you advices and comments.
    Last edited by Apfik; 04-12-2011 at 05:23 AM.

  2. #2
    Linux Newbie
    Join Date
    Apr 2010
    Location
    Novosibirsk, Russia
    Posts
    136

    Post

    It's quite simple to do with perl and regular expressions. Here is a script that reads your file and extracts numbers:

    Code:
    #!/usr/bin/perl
    
    use strict;
    
    # Suppose that yo pass an input file name as script parameter :
    my $file = $ARGV[0];
    
    # open file for reading or die, printing an error :
    open my $in, '<', $file or die "Cannot open $file: ".$!;
    
    # an array where script will store numbers
    my @strnumbers;
    # Operator <> reads lines from an open filehandle
    # and stores next line in $_ variable :
    while(<$in>) {
    # \s+ in regexp means any space character 
    # (e.g. space, tab, newline, carriage return), repeated one or more times
      my @tmplist = split /\s+/, $_;
      push @strnumbers, @tmplist;
    }
    
    # now @strnumbers contains all extracted numbers as is (e.g. "1.112224e+00")

  3. #3
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    This sounds a lot like a homework problem, which are not allowed on our forums:
    http://www.linuxforums.org/forum/lin...ums-rules.html

    This is going to be a fairly difficult problem to solve using only Bash. It essentially breaks down to two problems:

    1) How to extract the numbers
    2) How to convert them from scientific notation

    As Schmidt suggests, regular expressions are probably a good way to solve problem 1, and this can be done in just about any modern language, including Perl, Ruby, Python, Java, etc.

    Problem 2 is more difficult, but I suspect that at least one of the libraries above can handle scientific notation, or has a library available to do it.

    I'm going to close this thread because it sounds like a homework problem, but if you have any specific question on a specific concept, or if you have already written some code that you'd like advice on, feel free to start a new thread.
    Last edited by Cabhan; 04-12-2011 at 06:23 PM. Reason: OP contacted me explaining that this is not a homework problem. Reopened.
    DISTRO=Arch
    Registered Linux User #388732

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...