Sponsored Content
Top Forums Shell Programming and Scripting awk summing specific lines and fields Post 302725161 by nakaedu on Thursday 1st of November 2012 06:37:37 PM
Old 11-01-2012
awk summing specific lines and fields

Hi
I would like to know if it is possible to sum some specific fields.

I have this
Code:
 
x;x;x;x;x;x;x;x;467,390,611 Bytes;0.435291 GB;0.062247 GB;0.373045 GB;11,225;157
a;a;a;a;a;a;a;a;13,805,156,846 Bytes;12.857054 GB;1.838559 GB;11.018495 GB;151,063;18,933
b;b;b;b;b;b;b;b;232,797,478,723 Bytes;216.809547 GB;31.003765 GB;185.805782 GB;176,861;21,445;Parent: Line #545

I need to sum fields 9,10,11,12 except any line cotaining wording "Parent Line ...#

so currently I am doing something like grep -v -i "Parent" >new file,
then awk to print specific fields to new files,

and doing it through it.
Code:
 
awk "{s+=$1} END {print s}"

for this case desired output would be to sum only lines
Code:
 
x;x;x;x;x;x;x;x;467,390,611 Bytes;0.435291 GB;0.062247 GB;0.373045 GB;11,225;157
a;a;a;a;a;a;a;a;13,805,156,846 Bytes;12.857054 GB;1.838559 GB;11.018495 GB;151,063;18,933


Edit: adding more information:
I have a very long list nearly 1100 lines,
Each line is very similar to these

Code:
x;x;x;x;x;x;x;x;467,390,611 Bytes;0.435291 GB;0.062247 GB;0.373045 GB;11,225;157
a;a;a;a;a;a;a;a;13,805,156,846 Bytes;12.857054 GB;1.838559 GB;11.018495 GB;151,063;18,933
b;b;b;b;b;b;b;b;232,797,478,723 Bytes;216.809547 GB;31.003765 GB;185.805782 GB;176,861;21,445;Parent: Line #545

And I need to sum all fields to obtain: the total number of bytes, and GB. (while removing those lines that contain wording "Parent: Line"

For my example the total number of bytes and gigabytes, would be something similar to:
Code:
14272547457 Bytes;13.292345 GB;1.900806 GB;11.39154 GB

I get those numbers by summing all the row of fields $9, $10, $11, $12

467,390,611 Bytes + 13,805,156,846 Bytes = 14272547457 Bytes
0.435291 GB + 12.857054 GB = 13.292345 GB
0.062247 GB + 1.838559 GB = 1.900806 GB
0.373045 GB + 11.018495 GB = 11.39154 GB

Last edited by nakaedu; 11-02-2012 at 02:02 AM..
 

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

summing up the fields in fixed width file

Hi, I have a fixed width file with some records as given below: " 1000Nalsdjflj243324jljlj" "-0300Njfowjljl309933fsf" " 0010Njsfsjklj342344fsl" I want to sum-up first field values(i.e from 2nd character to 6th character)of each record. so for the above file i want to add (1000 - 300+... (2 Replies)
Discussion started by: srilaxmi
2 Replies

2. UNIX for Dummies Questions & Answers

Updating specific fields with awk using conditions

Can someone help me again, I think with awk? I have a file with 4 columns (pipe-delimited): I just want to convert the last field so that e1 is now 'message 1', e2 is 'message 2', e0 is 'message 3', etc. I don't want to change any other columns because the e0-e10 code may appear as part of a... (4 Replies)
Discussion started by: giannicello
4 Replies

3. Shell Programming and Scripting

Summing over specific lines and replacing the lines with the sum using sed, awk

Hi friends, This is sed & awk type question. I have a text file which has numbers spread all over the file. I want to sum the series of numbers whenever i find it and produce an output file with the sum. For example ###start of input text file #### abc def ghi 1 2 3 4 kjld random... (3 Replies)
Discussion started by: kaaliakahn
3 Replies

4. Shell Programming and Scripting

Summing over specific lines and replacing the lines with the sum

Hi friends, This is sed & awk type question. It is slightly different from my previous question. I have a text file which has numbers spread all over the file. I want to sum the series of numbers (but no more than 10 numbers in series) whenever i find it and produce an output file with the... (4 Replies)
Discussion started by: kaaliakahn
4 Replies

5. Shell Programming and Scripting

Summing all fields in a file

I was playing around to see how stuff works, and was trying to sum all fields in a file. cat file 1 2 3 4 5 6 7 8 9 10 11 12 I made this script: awk 'BEGIN {OFS=RS}{$1=$1}{s+=$0} END {print "sum="s}' file This gives 15, why not 78? I test it like this awk 'BEGIN... (5 Replies)
Discussion started by: Jotne
5 Replies

6. UNIX for Dummies Questions & Answers

Read the file and generate specific fields by awk

Hi I need to generate these output file from the below input file. Output : customer_id as customer, zip as zip_cd, catg_cd as catg, Input: out.customer::in.customer_id; out.zip_cd::in.zip; out.catg::in.catg_cd; Could you please help me on this. Please use code tags next... (1 Reply)
Discussion started by: Murugesh
1 Replies

7. Shell Programming and Scripting

Getting max value of specific fields with awk

Hello All, Here is am trying to get maximum value of third field depending on first,second and fourth fields with awk command . delimeter is pipe(|) . input 0221|09|14.25|aaa 0221|09|44.27|aaa 0221|09|44.33|aaa 0221|09|44.53|bbb 0221|09|34.32|bbb 0221|09|37.13|bbb... (5 Replies)
Discussion started by: sayami00
5 Replies

8. Shell Programming and Scripting

awk to combine lines if fields match in lines

In the awk below, what I am attempting to do is check each line in the tab-delimeted input, which has ~20 lines in it, for a keyword SVTYPE=Fusion. If the keyword is found I am splitting $3 using the . (dot) and reading the portion before and after the dot in an array a. If it does have that... (12 Replies)
Discussion started by: cmccabe
12 Replies

9. UNIX for Beginners Questions & Answers

Grep or awk a unique and specific word across many fields

Hi there, I have data with similar structure as this: CHR START-SNP END-SNP REF ALT PATIENT1 PATIENT2 PATIENT3 PATIENT4 chr1 69511 69511 A G homo hetero homo hetero chr2 69513 69513 T C . hetero homo hetero chr3 69814 69814 G C . . homo homo chr4 69815 69815 C A hetero . . hetero is... (10 Replies)
Discussion started by: daashti
10 Replies
asn1rt(3erl)						     Erlang Module Definition						      asn1rt(3erl)

NAME
asn1rt - ASN.1 runtime support functions DESCRIPTION
This module is the interface module for the ASN.1 runtime support functions. To encode and decode ASN.1 types in runtime the functions in this module should be used. EXPORTS
start() -> ok |{error,Reason} Types Reason = term() Starts the asn1 server that loads the drivers. The server schedules a driver that is not blocked by another caller. The driver is used by the asn1 application if specs are com- piled with options [per_bin, optimize] or [ber_bin, optimize, driver] . The server will be started automatically at encode/decode if it isn't done explicitly. If encode/decode with driver is used in test or industrial code it is a performance gain to start it explicitly to avoid the one time load in run-time. stop() -> ok |{error,Reason} Types Reason = term() Stops the asn1 server and unloads the drivers. decode(Module,Type,Bytes) -> {ok,Value}|{error,Reason} Types Module = Type = atom() Value = Reason = term() Bytes = binary | [Int] when integer(Int), Int >= 0, Int =< 255 | binary Decodes Type from Module from the list of bytes or binary Bytes . If the module is compiled with ber_bin or per_bin option Bytes must be a binary. Returns {ok,Value} if successful. encode(Module,Type,Value)-> {ok,BinOrList} | {error,Reason} Types Module = Type = atom() Value = term() BinOrList = Bytes | binary() Bytes = [Int|binary|Bytes] when integer(Int), Int >= 0, Int =< 255 Reason = term() Encodes Value of Type defined in the ASN.1 module Module . Returns a possibly nested list of bytes and or binaries if successful. If Module was compiled with the options per_bin and optimize the result is a binary. To get as fast execution as possible the encode function only performs rudimentary tests that the input Value is a correct instance of Type . The length of strings is for example not always checked. info(Module) -> {ok,Info} | {error,Reason} Types Module = atom() Info = list() Reason = term() info/1 returns the version of the asn1 compiler that was used to compile the module. It also returns the compiler options that was used. load_driver() -> ok | {error,Reason} Types Reason = term() This function loads the linked-in driver before the first call to encode. If this function is not called the driver will be loaded automatically at the first call to encode. If one doesn't want the performance cost of a driver load when the application is run- ning, this function makes it possible to load the driver in an initialization. The driver is only used when encoding/decoding ASN.1 files that were compiled with the options per_bin and optimize . unload_driver() -> ok | {error,Reason} Types Reason = term() This function unloads the linked-in driver. When the driver has been loaded it remains in the environment until it is unloaded. Nor- mally the driver should remain loaded, it is crucial for the performance of ASN.1 encoding. The driver is only used when ASN.1 modules have been compiled with the flags per_bin and optimize . utf8_binary_to_list(UTF8Binary) -> {ok,UnicodeList} | {error,Reason} Types UTF8Binary = binary() UnicodeList = [integer()] Reason = term() utf8_binary_to_list/1 Transforms a UTF8 encoded binary to a list of integers, where each integer represents one character as its unicode value. The function fails if the binary is not a properly encoded UTF8 string. utf8_list_to_binary(UnicodeList) -> {ok,UTF8Binary} | {error,Reason} Types UnicodeList = [integer()] UTF8Binary = binary() Reason = term() utf8_list_to_binary/1 Transforms a list of integers, where each integer represents one character as its unicode value, to a UTF8 encoded binary. validate(Module,Type,Value) -> ok | {error,Reason} Types Module = Type = atom() Value = term() Validates that Value conforms to Type from Module . Not implemented in this version of the ASN.1 application. Ericsson AB asn1 1.6.16 asn1rt(3erl)
All times are GMT -4. The time now is 09:08 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy