On Fri, Jun 20, 2008 at 7:32 AM, Yanto Young <[EMAIL PROTECTED]> wrote:
> Hi guys,
>
> I would like to know if there's a quick and dirty method (using
> bash/sed/awk) to perform the following:

I'm not that good with bash/sed/awk but yeah, it's possible, can't
outline the exact details, but roughly:

1) Set up a variable to hold the results
2) Use regular expression /(t=\d+\s)(\d+)/, the RegExp will make the
second number accessible easily, add that to the counter
3) Print the results.

Here is a Perl script that does that

#/usr/bin/perl

var result = 0;
open(FILE, '...') || die 'Can't open file';

while ( <FILE> ) {
  chomp;
  if ($_ =~ /(t=\d+\s)(\d+)/) {
    $result += $2;
    print $1, $result;
  } else {
    die 'File format is wrong.';
  }
}

Hope that helps (didn't test that though, but it should work). The
script output the result to stdout, but you can easily redirect that
using > or >> in command line. e.g.

$ ./transform input > output

>
> Original file:
> *******************
> t=0  1
> t=1  1
> t=2  -1
> t=3  1
> t=4  1
> t=5  -1
> *******************
>
> Desired result:
> *******************
> t=0  1
> t=1  2
> t=2  1
> t=3  2
> t=4  3
> t=5  2
> *******************
>
> that is, I would like to perform cumulative addition on the second column.
>
>
> Thanks.
>
> Best regards,
> Yanto




-- 
Chris
[EMAIL PROTECTED]
[EMAIL PROTECTED]

_______________________________________________
Slugnet mailing list
[email protected]
http://wiki.lugs.org.sg/LugsMailingListFaq
http://www.lugs.org.sg/mailman/listinfo/slugnet

Reply via email to