Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Adding in Awk

Shell Programming and Scripting


Closed    
 
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: 2 December 2016, 7:48 AM EST
Location: London, UK
Posts: 1,109
Thanks: 125
Thanked 258 Times in 242 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: 8 December 2016, 11:07 AM EST
Location: The Netherlands
Posts: 7,746
Thanks: 139
Thanked 558 Times in 520 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: 2 December 2016, 7:48 AM EST
Location: London, UK
Posts: 1,109
Thanks: 125
Thanked 258 Times in 242 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 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 06:46 AM
Adding new lines to a file + adding suffix to a pattern hemo21 Shell Programming and Scripting 7 06-14-2010 01:19 AM
adding nalakaatslt UNIX for Dummies Questions & Answers 1 11-07-2008 10:08 AM
adding 0 to a variable shivakundan Shell Programming and Scripting 3 02-14-2007 10:05 PM
Adding a new HDD jeffreydavisjr UNIX for Dummies Questions & Answers 5 10-16-2006 04:13 PM



All times are GMT -4. The time now is 07:30 AM.