LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Can I get awk to do this? (https://www.linuxquestions.org/questions/programming-9/can-i-get-awk-to-do-this-704547/)

jspaceman 02-13-2009 10:01 PM

Can I get awk to do this?
 
I have a text file (see below) that I want to perform some simple addition on. I want to take the number in the "CUM.RE.WORTH" column (19.3000), add it to the number in the "RE.WORTH" column, and put the result on the next line of the "CUM.RE.WORTH" column. I know how to get awk to read specific columns, but I'm not sure how to get it to start reading a column at a specific line, or append data to a specific column, etc.

Would awk be a good tool for this, or should I use something else?




Code:

              REACTIVITY EFFECTS OF LOADING CHANGES
 
 
  DATE OF SHUTDOWN : 2009-02-08
 
==========================================================================
| SITE    |  TYPE OF LOADING              |  RE.WORTH  | CUM.RE.WORTH |
|        |                                |    ( mk )    |    ( mk )  |
==========================================================================
                  REACTIVITY AT S/D                              19.3000
 
  M19            MRZ469                OUT        -1.0000
  M19            MRR401                IN          1.0000
  M13            MRL474                OUT        -1.0000
  M13            MRX402                IN          1.0000
  L22            MRG475                OUT        -1.0000
  L22            MRJ403                IN          1.0000
  M17            URR8553-4            OUT        0.8000
  M17            URR8573-4            IN        -0.8000
  L18            MRM477                OUT        -1.0000
  L18            MRS404                IN          1.0000
  M15            MRV478                OUT        -1.0000
  M15            MRW405                IN          1.0000
  L08            S340B267              OUT        -7.3000
  L28            FL1608                OUT        -2.5000
  L28            FL1580                IN          4.2000
  M17            DMY-5                OUT        0.2000
  K15            URR8570-6            OUT        0.8000
  K15            DMY-5                IN        -0.2000
  M17            URR8570-5            IN        -0.8000
  L08            HFD3B                IN          2.0000
  B11            FL1633                OUT        -3.2000
  B11            FLNEW                IN          4.4000
  L04            FL1564                OUT        -1.4000
  L04            FL1633                IN          4.9000
  K17            FL1605                OUT        -4.1000
  K17            FL1599                IN          5.9000


anomie 02-13-2009 10:04 PM

(I think) I see what you're asking. You want to have a running total on the far right column? How about posting what you've tried so far?

To answer your more general question, you could probably kludge together an awk script to handle this problem. (i.e. I don't think it's beyond awk's capabilities.)

paulsm4 02-14-2009 02:35 AM

Awk can do it ...

... but Perl would probably be a much easier

IMHO .. PSM

colucix 02-14-2009 02:59 AM

Code:

/CUM.RE.WORTH/ {
    print ; getline
    print ; getline
    print ; getline
    num = $NF
}

/\yOUT\y|\yIN\y/ {
    head = substr($0,1,50)
    sum = $NF + num
    gsub($0,sprintf("%s %8.4f", head, sum))
}
   
{ print }


ghostdog74 02-14-2009 08:07 AM

Code:

awk '/REACTIVITY AT S\/D/{n=$NF}$3~/IN|OUT/{$NF=$NF+n}1' file

colucix 02-14-2009 09:57 AM

ghostdog74, this time your incredibly short one-liner does not preserve spacing in lines containing IN/OUT. colucix b. ghostdog 2-1 :D

ghostdog74 02-14-2009 11:30 PM

Quote:

Originally Posted by colucix (Post 3443297)
ghostdog74, this time your incredibly short one-liner does not preserve spacing in lines containing IN/OUT. colucix b. ghostdog 2-1 :D

lol. i leave it to the OP then.. :)


All times are GMT -5. The time now is 02:26 AM.