Visit Our UNIX and Linux User Community

awk or a combination of commands to read and calculate nth lines from pattern

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk or a combination of commands to read and calculate nth lines from pattern
# 1  
Old 02-03-2019
awk or a combination of commands to read and calculate nth lines from pattern

Two numerical lines, found by either header line, need to be added
and the total placed in a new-header section. Also the total should
should be rounded or cut to a two decimal anynumber.XX format
with the AB string added on the end.

For example: The numerical lines from headers 2 and 3 are to be added together
and placed in the 4th line of the new-header grouping.
The new line would show 13.24 AB

The data file:
Each grouping contains 4 lines.
The line numbers may change every time the data file is accessed.
The numerical lines may or may not have a type (AB in this example) listed after the number.
The third line in the group, the info line, may or may not be the same.

Data file:

header1 - description
1.23 AB
header2 - description
10.678 AB
header3 - description

A new section would then be added to the end of the data with no empty line above:

new-header - description
13.24 AB

Last edited by jessandr; 02-03-2019 at 02:58 AM..
# 2  
Old 02-03-2019
Hello jessandr,

Not that much clear, please do mention why 1st value of header is NOT being picked up?

R. Singh
# 3  
Old 02-03-2019
Welcome to the forum.

Please become accustomed to supplying sufficient info on and context of your problem. On top of what RavinderSingh13 already said:
- Where (in a larger file) should the new record be placed?
- How should the new info line ("info-xyz" in your sample record) be constructed?
- What be the header name if more than one new headers result?
# 4  
Old 02-07-2019
Start with:

awk '
! (NR % 4) {if (length($2)) type=$2;}
! (NR % 12) || ! ((NR + 4) % 12) {sum+=$1;}
! (NR % 12) {
   print "new-header";
   print "new-header - description";
   print "info-xyz";
   printf "%.2f %s\n", sum, type;
' data_file

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk to remove pattern and lines above pattern

In the awk below I am trying to remove all lines above and including the pattern Test or Test2. Each block is seperated by a newline and Test2 also appears in the lines to keep but it will always have additional text after it. The Test to remove will not. The awk executed until the || was added... (2 Replies)
Discussion started by: cmccabe
2 Replies

2. Shell Programming and Scripting

Optimize a combination of commands

Im currently running this command to satisfy a particular task. it works for my purposes. but i want to be able to optimize this string of commands and have it be reduced to 1 or 2 commands, if at all possible: head -4 datafile 2>/dev/null | cut -c1-400 | wc | awk '{print $2$1$3}' (5 Replies)
Discussion started by: SkySmart
5 Replies

3. Shell Programming and Scripting

Help with parsing file with combination of pattern

I have a file1 like prt1|als28.1 prt3|als53.1 prt2|als550.1 prt1|bls9.2 prt2|als7.2 prt2|bls0.2 prt2|als872.1 prt1|bls871.1 prt2|als6.2 prt4|als22.1 prt2|bls43.2 I want to create a file2 from this file by comparing all the possible combinations of patterns (prt) assuming prt1... (3 Replies)
Discussion started by: sammy777
3 Replies

4. Shell Programming and Scripting

Insert new pattern in newline after the nth occurrence of a line pattern - Bash in Ubuntu 12.04

Hi, I am getting crazy after days on looking at it: Bash in Ubuntu 12.04.1 I want to do this: pattern="system /path1/file1 file1" new_pattern=" data /path2/file2 file2" file to edit: data.db - I need to search in the file data.db for the nth occurrence of pattern - pattern must... (14 Replies)
Discussion started by: Phil3759
14 Replies

5. Shell Programming and Scripting

Using AWK to find top Nth values in Nth column

I have an awk script to find the maximum value of the 2nd column of a 2 column datafile, but I need to find the top 5 maximum values of the 2nd column. Here is the script that works for the maximum value. awk 'BEGIN { subjectmax=$1 ; max=0} $2 >= max {subjectmax=$1 ; max=$2} END {print... (3 Replies)
Discussion started by: ncwxpanther
3 Replies

6. Shell Programming and Scripting

search pattern and read lines

Hi, I have a huge de-limited file which has pattern : 99"9876"2010-11-21 12:51:01"J"MNOPQRS ID# 2-1234-1234-0099-9876-0 "" <<read>> 99"9876"2010-11-21 12:51:01"K"R-EMP# 01234567 (LOGOFF) "" <<read>> 99"9876"2010-11-21 12:51:01"L" *AUTO LOGOFF* ... (3 Replies)
Discussion started by: angie1234
3 Replies

7. Shell Programming and Scripting

read lines between search pattern

I have a file split something like 01/11/2010: No of users 100 02/11/2010: No of users 102 03/11/2010: No of users 99 ... I want to search the file for a particular date and then extract the following line with the date, something like 02/11/2010 No of users 102 I can grep... (6 Replies)
Discussion started by: gefa
6 Replies

8. Shell Programming and Scripting

Advice using cut & echo combination commands

Hi, I am cutting data from a fixed length test file and then writing out a new record using the echo command, the problem I have is how to stop multiple spaces from being written to the output file as a single space. Example: cat filea | while read line do field1=`echo $line | cut -c1-2` ... (6 Replies)
Discussion started by: dc18
6 Replies

9. UNIX for Dummies Questions & Answers

combination of two commands

I want to show a output like this Lee Ballancore PID TTY TIME CMD 31799 pts/3 00:00:00 vim 31866 pts/3 00:00:00 vim 2495 pts/7 00:00:00 vim 8368 pts/0 00:00:00 vim 9544 pts/2 00:00:00 ps Alistairr Rutherford PID TTY TIME CMD 8368 pts/0 00:00:00 vim 9544 pts/2 00:00:00 ps ... (3 Replies)
Discussion started by: nehaquick
3 Replies

10. UNIX for Dummies Questions & Answers

Combination Of commands

Hello All, I just wanted to know what are the different ways of using commands in combination. The most common one which i know is using pipes. Also grouping is also done like ( ls; date) where output of both the commands is displayed. Are there any other ways of combining various... (2 Replies)
Discussion started by: rahulrathod
2 Replies

Featured Tech Videos