Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

Adding in Awk

Shell Programming and Scripting


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 03-28-2012
Leo_Boon Leo_Boon is offline
Registered User
 
Join Date: Mar 2012
Last Activity: 16 October 2012, 10:44 AM EDT
Posts: 15
Thanks: 6
Thanked 0 Times in 0 Posts
Adding in Awk

Hello,
suppose I have a file that consists of a single column of various numbers, as in

Code:
12.010
1.0080
1.0080
0.8780
0.1350
0.0000
-0.4157
0.2719

How can I use AWK (or equivalent) to add the numbers of two specific lines? I want to sum, for example, the first with the fifth, the second with the sixth and so on.
Thanks in advance.
Sponsored Links
    #2  
Old Unix and Linux 03-28-2012
CarloM CarloM is offline Forum Advisor  
Registered User
 
Join Date: Oct 2011
Last Activity: 26 June 2015, 7:44 AM EDT
Location: London, UK
Posts: 1,102
Thanks: 122
Thanked 255 Times in 239 Posts
You could do something like:

Code:
awk '{ lines[NR]=$0 } END { print lines[1]+lines[5] }' filename

The Following User Says Thank You to CarloM For This Useful Post:
Leo_Boon (03-28-2012)
Sponsored Links
    #3  
Old Unix and Linux 03-28-2012
Franklin52's Unix or Linux Image
Franklin52 Franklin52 is offline Forum Staff  
Moderator
 
Join Date: Feb 2007
Last Activity: 29 June 2015, 6:05 AM EDT
Location: The Netherlands
Posts: 7,730
Thanks: 129
Thanked 545 Times in 513 Posts

Code:
awk 'NR < 5{a[NR%5]=$0; next}{print $0 + a[NR%5+1]}' file


Use nawk or /usr/xpg4/bin/awk on Solaris if you get errors.
The Following User Says Thank You to Franklin52 For This Useful Post:
Leo_Boon (03-28-2012)
    #4  
Old Unix and Linux 03-28-2012
Leo_Boon Leo_Boon is offline
Registered User
 
Join Date: Mar 2012
Last Activity: 16 October 2012, 10:44 AM EDT
Posts: 15
Thanks: 6
Thanked 0 Times in 0 Posts
Thanks. How can I do it iteratively? Say, every line with one 4 lines ahead of it?

---------- Post updated at 03:45 PM ---------- Previous update was at 03:34 PM ----------

I hadn't refreshed and I hadn't seen the post above mine before posting...
What does [NR%5] mean?
Sponsored Links
    #5  
Old Unix and Linux 03-28-2012
CarloM CarloM is offline Forum Advisor  
Registered User
 
Join Date: Oct 2011
Last Activity: 26 June 2015, 7:44 AM EDT
Location: London, UK
Posts: 1,102
Thanks: 122
Thanked 255 Times in 239 Posts
NR is a built-in awk variable for the current record (line) number.

%5 is modulo 5 (remainder after dividing by 5, integer arithmetic).
Sponsored Links
Closed Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
adding new ip newaix AIX 1 10-21-2011 05:46 AM
Adding new lines to a file + adding suffix to a pattern hemo21 Shell Programming and Scripting 7 06-14-2010 12:19 AM
adding nalakaatslt UNIX for Dummies Questions & Answers 1 11-07-2008 09:08 AM
adding 0 to a variable shivakundan Shell Programming and Scripting 3 02-14-2007 09:05 PM
Adding a new HDD jeffreydavisjr UNIX for Dummies Questions & Answers 5 10-16-2006 03:13 PM



All times are GMT -4. The time now is 12:49 PM.