Adding in Awk | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Adding in Awk

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 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 03-28-2012
CarloM CarloM is offline
Registered User
 
Join Date: Oct 2011
Last Activity: 23 April 2014, 1:21 PM EDT
Location: London, UK
Posts: 929
Thanks: 72
Thanked 214 Times in 201 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 03-28-2012
Franklin52's Avatar
Franklin52 Franklin52 is offline Forum Staff  
Moderator
 
Join Date: Feb 2007
Last Activity: 24 April 2014, 10:18 AM EDT
Location: The Netherlands
Posts: 7,668
Thanks: 116
Thanked 525 Times in 496 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 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 03-28-2012
CarloM CarloM is offline
Registered User
 
Join Date: Oct 2011
Last Activity: 23 April 2014, 1:21 PM EDT
Location: London, UK
Posts: 929
Thanks: 72
Thanked 214 Times in 201 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 Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 03:15 PM.