Sponsored Content
Top Forums Shell Programming and Scripting Linux - Calculations between multiple rows of data Post 302965182 by RavinderSingh13 on Monday 25th of January 2016 06:10:14 AM
Old 01-25-2016
Hello RichZR,

Thank you for using code tags as per forum rulesSmilie. You could use code tags for sample Inputs too which you have shown us in you post.
Following may help you in same.
For your 1st requirement following may help you:
Code:
awk -F"|" 'NR==1{$(NF+1)="col6";print;next} NR==2{$(NF)=100;VAL=$(NF-1);print;next} {$(NF)=$(NF-1) * 100/VAL;print}' OFS="|"  Input_file > Output_file

Output will be as follows.
Code:
Col1|Col2|Col3|Col4|Col5|col6
ABC|00000001|15-Dec-15|13,400|110,000|100
ABC|00000001|31-Jan-16|13,490|80,000|72.7273
ABC|00000001|29-Feb-16|13,500|77,000|70
ABC|00000001|31-May-16|40,200|37,000|33.6364
ABC|00000001|31-Aug-16|42,000|0|0
ABC|00000002|15-Dec-15|13,400|110,000|100
ABC|00000002|31-Jan-16|13,490|80,000|72.7273
ABC|00000002|29-Feb-16|13,500|77,000|70
ABC|00000002|31-May-16|40,200|37,000|33.6364
ABC|00000002|31-Aug-16|42,000|0|0

Now for your 2nd requirement you could try as follows. Let's say we have taken output of 1st requirement into a file named Output_file.
Code:
awk -F"|" 'NR==1{$(NF+1)="col7";print;next} {B[++i]=$0;A[i]=$NF} END{for(i=1;i<NR;i++){VAL=(A[i] + A[i+1])/2;print B[i] FS VAL}}' OFS="|"   Output_file

Output will be as follows.
Code:
Col1|Col2|Col3|Col4|Col5|col6|col7
ABC|00000001|15-Dec-15|13,400|110,000|100|86.3637
ABC|00000001|31-Jan-16|13,490|80,000|72.7273|71.3637
ABC|00000001|29-Feb-16|13,500|77,000|70|51.8182
ABC|00000001|31-May-16|40,200|37,000|33.6364|16.8182
ABC|00000001|31-Aug-16|42,000|0|0|50
ABC|00000002|15-Dec-15|13,400|110,000|100|86.3637
ABC|00000002|31-Jan-16|13,490|80,000|72.7273|71.3637
ABC|00000002|29-Feb-16|13,500|77,000|70|51.8182
ABC|00000002|31-May-16|40,200|37,000|33.6364|16.8182
ABC|00000002|31-Aug-16|42,000|0|0|0

Thanks,
R. Singh
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

extract multiple cloumns from multiple files; skip rows and include filenames; awk

Hello, I am trying to write a bash shell script that does the following: 1.Finds all *.txt files within my directory of interest 2. reads each of the files (25 files) one by one (tab-delimited format and have the same data format) 3. skips the first 10 rows of the file 4. extracts and... (4 Replies)
Discussion started by: manishabh
4 Replies

2. UNIX for Dummies Questions & Answers

Converting rows into multiple-rows

Hi every one; I have a file with 22 rows and 13 columns which includes floating numbers. I want to parse the file so that every five columns in the row would be a new record (row). For example, the first line in the old file should be converted into three lines with first two lines contain 5... (6 Replies)
Discussion started by: PHL
6 Replies

3. Shell Programming and Scripting

Group search (multiple data points) in Linux

Hi All I have a data set like this tab delimited: weft fgr-1 345 -1 fgrythdgd weft fgr-3 456 -2 ghjdklflllff weft fgr-11 456 -3 ghtjuffl weft fgr-1 213 -2 ghtyjdkl weft fgr-34 567 -5 fghytkflf frgt fgr-36 567 -1 ghrjufjf frgt fgr-45 678 -2 ghjruir frgt fgr-34 546 -5 gjjjgkldlld frgt... (4 Replies)
Discussion started by: Lucky Ali
4 Replies

4. Shell Programming and Scripting

Split single rows to multiple rows ..

Hi pls help me out to short out this problem rm PAB113_011.out rm: PAB113_011.out: override protection 644 (yes/no)? n If i give y it remove the file. But i added the rm command as a part of ksh file and i tried to remove the file. Its not removing and the the file prompting as... (7 Replies)
Discussion started by: sri_aue
7 Replies

5. UNIX for Dummies Questions & Answers

Formatting Multiple fields on 1 line to multiple rows

I'm trying extract a number of filename fields from a log file and copy them out as separate rows in a text file so i can load them into a table. I'm able to get the filenames but the all appear on one line. I tried using the cut command with the -d (delimiter) option but cant seem to make it... (1 Reply)
Discussion started by: Sinbad-66
1 Replies

6. UNIX for Dummies Questions & Answers

help with doing calculations on data

Dear All, I have a long list like this: 337 375 364 389 443 578 1001 20100 . . . . etc I would like to substract each value from the first entry which in this case is 337 and report it in a separate column. So the expected output looks like 337 0 (10 Replies)
Discussion started by: pawannoel
10 Replies

7. UNIX for Dummies Questions & Answers

Suggestion to convert data in rows to data in columns

Hello everyone! I have a huge dataset looking like this: nameX nameX 0 1 2 2 2 2 2 2 2 2 2 2 2 2 1 2 2 2 1 2 2 2 ............... nameY nameY 2 2 2 2 2 2 2 2 2 2 1 2 2 2 1 2 2 2 ..... nameB nameB 0 1 2 2 2 2 2 2 2 2 1 2 2 2 1 2 2 2 ..... (can be several thousands of codes) and I need... (8 Replies)
Discussion started by: kush
8 Replies

8. Shell Programming and Scripting

Create Multiple UNIX Files for Multiple SQL Rows output

Dear All, I am trying to write a Unix Script which fires a sql query. The output of the sql query gives multiple rows. Each row should be saved in a separate Unix File. The number of rows of sql output can be variable. I am able save all the rows in one file but in separate files. Any... (14 Replies)
Discussion started by: Rahul_Bhasin
14 Replies

9. Shell Programming and Scripting

Extracting data from specific rows and columns from multiple csv files

I have a series of csv files in the following format eg file1 Experiment Name,XYZ_07/28/15, Specimen Name,Specimen_001, Tube Name, Control, Record Date,7/28/2015 14:50, $OP,XYZYZ, GUID,abc, Population,#Events,%Parent All Events,10500, P1,10071,95.9 Early Apoptosis,1113,11.1 Late... (6 Replies)
Discussion started by: pawannoel
6 Replies

10. UNIX for Beginners Questions & Answers

How to transpose pieces of data in a column to multiple rows?

Hello Everyone, I am very new to the world of regular expressions. I am trying to use grep/sed for the following: Input file is something like this and there are multiple such files: abc 1 2 3 4 5 ***END*** abc 6 7 8 9 ***END*** abc 10 (2 Replies)
Discussion started by: shellnewuser
2 Replies
IGAWK(1)							 Utility Commands							  IGAWK(1)

NAME
igawk - gawk with include files SYNOPSIS
igawk [ all gawk options ] -f program-file [ -- ] file ... igawk [ all gawk options ] [ -- ] program-text file ... DESCRIPTION
Igawk is a simple shell script that adds the ability to have ``include files'' to gawk(1). AWK programs for igawk are the same as for gawk, except that, in addition, you may have lines like @include getopt.awk in your program to include the file getopt.awk from either the current directory or one of the other directories in the search path. OPTIONS
See gawk(1) for a full description of the AWK language and the options that gawk supports. EXAMPLES
cat << EOF > test.awk @include getopt.awk BEGIN { while (getopt(ARGC, ARGV, "am:q") != -1) ... } EOF igawk -f test.awk SEE ALSO
gawk(1) Effective AWK Programming, Edition 1.0, published by the Free Software Foundation, 1995. AUTHOR
Arnold Robbins (arnold@skeeve.com). Free Software Foundation Nov 3 1999 IGAWK(1)
All times are GMT -4. The time now is 06:10 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy