Analyzing last 2 fields of 1 row and 3rd field of next row

Login or Register to Reply

Thread Tools Search this Thread
# 1  
Analyzing last 2 fields of 1 row and 3rd field of next row

I have the following script that will average the last two fields of each row, but im not sure how to include the 3rd field of the following row.

An example of the analysis that I need to perform from the input -

awk '{sum=cnt=0; for (i=13;i<=NF;i++) { sum+=$i; cnt++ } { print $1, $2, sum/cnt } }' input

01001   271895  53.71   48.74   67.64   76.39   81.90   89.22   91.06   90.37   90.91   75.99   66.61   58.01
01001   271896  54.16   60.85   65.32   81.61   88.54   88.16   92.03   94.50   90.75   77.18   69.87   58.69
01001   271897  54.18   63.10   71.44   75.06   83.16   95.63   93.34   89.89   88.88   81.32   68.14   58.77
01001   271898  60.55   59.13   70.97   71.98   89.51   93.87   91.47   88.79   86.67   73.62   61.66   55.71

01001 271895 62.31
01001 271896 64.28
01001 271897 63.45

Desired Output
01001 271895 59.59
01001 271896 60.91
01001 271897 62.49

Any tips?
# 2  
For exactly your above laid out problem, try
awk 'NR > 1 {print LEADIN, (sum+$3)/3} {LEADIN = $1 OFS $2; sum = $13 + $14} ' file
01001 271895 59.5933
01001 271896 60.9133
01001 271897 62.4867

The output format can be modified by setting the OFMT variable.
For a two field summation, a for loop is not necessary, and you don't need a count if you're always adding three elements. If, on the other hand, you want a more generic approach, combine your attempt with this one.
These 2 Users Gave Thanks to RudiC For This Post:
Login or Register to Reply

Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
Splitting single row into multiple rows based on for every 10 digits of last field of the row
Hi ALL, We have requirement in a file, i have multiple rows. Example below: Input file rows 01,1,102319,0,0,70,26,U,1,331,000000113200000011920000001212 01,1,102319,0,1,80,20,U,1,241,00000059420000006021 I need my output file should be as mentioned below. Last field should split for...... Shell Programming and Scripting
Shell Programming and Scripting
Keep only the closet match of timestamped row (include headers) from file1 to precede file2 row/s
My original files are like this below and I distinguish them from the AP_ID (file1 has 572 and file2 has 544). Also, the header on file1 has “G_” pre-pended. NOTE: these are only snippets of very large files and much of the data is not present here. Original File 1: ...... UNIX for Beginners Questions & Answers
UNIX for Beginners Questions & Answers
Add Row from First Row (Split Row)
HI Guys, I have Below Input :- RepigA_hteis522 ReptCfiEtrBsCll_aofe MSL04_MSL2_A25_1A 0 9 MSL04_MSL2_A25_1B 0 9 MSL04_MSL2_A25_1C 0 9 RepigA ReptCfiEtrBsCll hteis522 aofe MSL04_MSL2_A25_1A 0 9 MSL04_MSL2_A25_1B 0 9 MSL04_MSL2_A25_1C 0 9 Split Data in two first row...... Shell Programming and Scripting
Shell Programming and Scripting
how to spilit a row into fields and store the field content to the array
consider this is a line A#B#C#D#E#F#G#H note the delimeter is # i want to cut or spilt in to fields using the delimeter # and to store in an array. like this array=A array=B array=C array=D array=E and the array content should be displayed. echo "${array}" echo "${array}"...... Shell Programming and Scripting
Shell Programming and Scripting
How to insert data befor some field in a row of data depending up on values in row
Hi I need to do some thing like "find and insert before that " in a file which contains many records. This will be clear with the following example. The original data record should be some thing like this 60119827 RTMS_LOCATION_CDR INSTANT_POSITION_QUERY 1236574686123083rtmssrv7 ...... Shell Programming and Scripting
Shell Programming and Scripting
Red Hat Linux

Red Hat, Inc. is a global American software company providing open-source software products to the enterprise community. Red Hat was Founded in 1993, and has its corporate headquarters in Raleigh, North Carolina, with other offices worldwide. On October 28, 2018, IBM announced its intent to acquire Red Hat for $34 billion. Red Hat has focused on Red Hat Enterprise Linux an enterprise operating system. Red Hat also acquired the open-source enterprise middleware vendor JBoss. Red Hat also sells an enterprise virtualization product, Red Hat Virtualization (RHV). Red Hat also provides storage, operating system platforms, middleware, applications, management products, and support, training, and consulting services. Red Hat actively creates, maintains, and contributes to many free and open software projects. Red Hat has acquired several proprietary software products through corporate mergers and acquisitions and has released these software acquisitions under open-source licenses.
Please enjoy and help our forum community by "showing your work" and posting your code, even when your code is not working as expected. To help others help you, please wrap your code blocks, sample input, sample output, error messages, and other data in CODE tags and wrap your short commands and short data objects in ICODE tags. We were all beginners in the beginning. If you have any questions about how to register or how to post, please contact us in Live Chat. Thank you and enjoy this "forever free" technical support community for UNIX, Linux and computer information technology in general.