LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-13-2009, 10:01 PM   #1
jspaceman
Member
 
Registered: Dec 2000
Location: Toronto, Canaduhhhh
Distribution: Slackware -current, OpenBSD 3.8-stable
Posts: 178

Rep: Reputation: 30
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
 
Old 02-13-2009, 10:04 PM   #2
anomie
Senior Member
 
Registered: Nov 2004
Location: Texas
Distribution: RHEL, Scientific Linux, Debian, Fedora
Posts: 3,935
Blog Entries: 5

Rep: Reputation: Disabled
(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.)
 
Old 02-14-2009, 02:35 AM   #3
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Awk can do it ...

... but Perl would probably be a much easier

IMHO .. PSM
 
Old 02-14-2009, 02:59 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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 }
 
Old 02-14-2009, 08:07 AM   #5
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Code:
awk '/REACTIVITY AT S\/D/{n=$NF}$3~/IN|OUT/{$NF=$NF+n}1' file
 
Old 02-14-2009, 09:57 AM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
ghostdog74, this time your incredibly short one-liner does not preserve spacing in lines containing IN/OUT. colucix b. ghostdog 2-1
 
Old 02-14-2009, 11:30 PM   #7
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by colucix View Post
ghostdog74, this time your incredibly short one-liner does not preserve spacing in lines containing IN/OUT. colucix b. ghostdog 2-1
lol. i leave it to the OP then..
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
awk question on handling *.CSV "text fields" in awk jschiwal Programming 8 05-27-2010 06:23 AM
awk , I need help for awk, just a display function mcandy General 1 12-15-2008 12:21 PM
using awk on a awk result jpgauvin Programming 1 12-15-2008 03:57 AM
shell command using awk fields inside awk one71 Programming 6 06-26-2008 04:11 PM
Some comments on awk and awk scripts makyo Programming 4 03-02-2008 05:39 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:01 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration