Adding the corresponding values for every 5th consecutive numbers


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Adding the corresponding values for every 5th consecutive numbers
# 1  
Old 11-05-2013
Adding the corresponding values for every 5th consecutive numbers

Dear All,

I have a file which is as follows:

Input File:

Code:
231    100.1
233     99
235     200.9
238     80.1
239     90.2
240     77.0
243     99.5
245     16.20
246     13.55
247     11.8
249     13.7
250     99.6

Expected Output:

Code:
231    400
237    247.3
243    141.05
249    113.3

Logic: The 1st column is printing every 5th number in the consecutive order(does not matter if it is not present in the input file). But it starts with the 1st number of the Column 1 and then prints every 5th number.

The second column prints the addition of all the 5 numbers. For example, 232 is not present, therefore we consider 0 corresponding to 232.

I am new in the field and would appreciate tour help.
# 2  
Old 11-05-2013
The logic you describe and the sample you give do not coincide. Anyhow, using 6 as the delta to increase your field 1 counter, try
Code:
awk     'NR==1          {printf "%s\t",$1; N=$1+6}
         $1>=N          {printf "%s\n%s\t",sum,N; N+=6; sum=0}
                        {sum+=$2}
         END            {printf "%s\n", sum}
        ' file
231    400
237    247.3
243    141.05
249    113.3

This User Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Check/print missing number in a consecutive range and remove duplicate numbers

Hi, In an ideal scenario, I will have a listing of db transaction log that gets copied to a DR site and if I have them all, they will be numbered consecutively like below. 1_79811_01234567.arc 1_79812_01234567.arc 1_79813_01234567.arc 1_79814_01234567.arc 1_79815_01234567.arc... (3 Replies)
Discussion started by: newbie_01
3 Replies

2. UNIX for Dummies Questions & Answers

Sum every 3 consecutive numbers in a column

Dear All, I have a file with only one column. And I want to add every 3 consecutive numbers together and print the result. Input File: 21.1 10 10 55 11 99 10 8 4 Expected Output: 41.1 (5 Replies)
Discussion started by: NamS
5 Replies

3. UNIX for Dummies Questions & Answers

How to combine and insert missing consecutive numbers - awk or script?

Hi all, I have two (2) sets of files that are based on some snapshots of database that I want to merge and insert any missing sequential number. Below are example representation of these files: file1: DATE TIME COL1 COL2 COL3 COL4 ID 01/10/2013 0800 100 ... (3 Replies)
Discussion started by: newbie_01
3 Replies

4. Shell Programming and Scripting

Disruption of consecutive numbers

I do have a tab delimited file with the following format 200 46 201 67 204 89 205 98 206 89 208 890 210 23 .. ... 100's of rows I would like to output the missing consecutive number of the first column. The expected output will be: (1 Reply)
Discussion started by: Lucky Ali
1 Replies

5. Shell Programming and Scripting

Print consecutive numbers in column2

Hi, I have an input file of the following style input.txt The 4000 at the end indicates the total no. of columns in that row. I would like to replace all -1s with consecutive 1 and 2 and print the whole line again. So, the output would be output.txt Thanks in advance. (7 Replies)
Discussion started by: jacobs.smith
7 Replies

6. Shell Programming and Scripting

Finding consecutive numbers in version names on a txt file

Hi all. I have a directory which contains files that can be versioned. All the files are named according to a pattern like this: TEXTSTRING1-001.EXTENSION TEXTSTRING2-001.EXTENSION TEXTSTRING3-001.EXTENSION ... TEXTSTRINGn-001.EXTENSION If a file is versioned, a file called ... (10 Replies)
Discussion started by: fox1212
10 Replies

7. Programming

FORTRAN: Getting Consecutive DIST Values

I have this code and I want to get the first two consecutive distances (obtained by calling GET_TH(IT, 'DIST', DIST) at index IT, the result stored in DIST) for which (ITFLG(IT) .NE. 1). L_FIRST get the first DIST for which ITFLG(IT) .NE. 1. Getting a bit confused on how to achieve this. ... (0 Replies)
Discussion started by: kristinu
0 Replies

8. Shell Programming and Scripting

concatenate consecutive field values

Hi, I have a file like this A Bob A Sam A John B David C Paul C Sandra If the consecutive field values in column one is same, then concatenate the corresponding strings. So, I need an output like this, A Bob_Sam_John B David C Paul_Sandra I usually work with excel but... (3 Replies)
Discussion started by: polsum
3 Replies

9. Shell Programming and Scripting

Inserting a range of consecutive numbers into a text file

I have a text file in the following format .... START 1,1 2,1 3,1 .. .. 9,1 10,1 END .... I want to change to the output to .... START 1,1 2,1 3,1 .. (4 Replies)
Discussion started by: VNR
4 Replies
Login or Register to Ask a Question