Sponsored Content
Top Forums Shell Programming and Scripting Sum fields of different files using awk Post 302836035 by Yoda on Tuesday 23rd of July 2013 11:57:12 AM
Old 07-23-2013
Code:
awk '
        # Create an associative array: A for which value is sum of $2 and indexed by $1
        {
                A[$1] += $2
        }

        # End Block
        END {
        # For each element in associative array: A
                for ( k in A )
                        # Print index & value of
                        print k, A[k]
        }
# Path name expansion (aka globbing) will help open & read all files with file name prefixed: file
' file*

This User Gave Thanks to Yoda For This Post:
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sort sum fields

HI ALL, i have a problem when i do a sort sum with many fields. Is there a limit for fields? Do you know a solution? thanks in advance. the shell is: # SORT1 SORT1_rcode=777 if ; then echo "USE $DARSEQ/OTPU.FTPEPREC RECORD F,1000 " > $DARPARSRT/TPEKL508.SORT1_$$.srt ... (6 Replies)
Discussion started by: tullo
6 Replies

2. Shell Programming and Scripting

calculating sum of fields in a file

Hey, I have a file and it has only one field. I need to calculate the sum of each filed as total. For e.g my file is 1 2 3 4 5 I need to calculate the total sum as 15. Please let me know how i can do it? (4 Replies)
Discussion started by: dsravan
4 Replies

3. Shell Programming and Scripting

Compare fields in 2 files using AWK

Hi unix gurus, I have a urgent requirement, I need to write a AWK script to compare each fields in 2 files using AWK. Basically my output should be like this. file1 row|num1|num2|num3 1|one|two|three 2|one|two|three file2 row|num1|num2|num3 1|one|two|three 2|one|two|four ... (5 Replies)
Discussion started by: rashmisb
5 Replies

4. Programming

comparing two fields from two different files in AWK

Hi, I have two files formatted as following: File 1: (user_num_ID , realID) (the NR here is 41671) 1 cust_034_60 2 cust_80_91 3 cust_406_4 .. .. File 2: (realID , clusterNumber) (total NR here is 1000) cust_034_60 2 cust_406_4 3 .. .. (11 Replies)
Discussion started by: amarn
11 Replies

5. Shell Programming and Scripting

To perform sum aggregation on numeric fields

Hi all, I have following scenario to perform sum aggregation on certain columns Node Allocated_Space Pool_Name CS_Group Utilized Space -------- ---------------- ---------- --------- -------------- bdw1a_lun01 300 bdw_p0 ... (2 Replies)
Discussion started by: ckwan
2 Replies

6. Shell Programming and Scripting

Need help in finding sum for values in 2 different fields

Hi there, I have 2 files in following format cat file_1 Storage Group Name: aaaa HBA UID SP Name SPPort ------- ------- ------ 0 21 Storage Group Name: bbbb HBA UID... (2 Replies)
Discussion started by: jpkumar10
2 Replies

7. Shell Programming and Scripting

Need to sum up a column value from multiple files into Credit and Debit categories using awk command

i have multiple files with Batch Header, Record detail & Batch trailer data in the files like : BH 20150225950050N8262 RD 20140918000000 99999999 unk Deferred Settlement -13950 BT01 -13950 *Above sample data donot have the spaces coorectly defined. I do have multiple batch trailer... (1 Reply)
Discussion started by: kcdg859
1 Replies

8. Shell Programming and Scripting

awk to comppare two files using rwo fields

I use the two awk scripts for comparing file1 and file2. First awk compare $3 column: awk -v OFS="\t" 'NR==FNR{a=$4;next}{$2=$2 "\t"(a?a:"-")}1' file1 file2Second awk compare $2 column: awk -v OFS="\t" 'NR==FNR{a=$4;next}{$2=$2 "\t"(a?a:"-")}1' file1 file2 The only difference ... (12 Replies)
Discussion started by: cmccabe
12 Replies

9. Shell Programming and Scripting

awk to update file with sum of matching fields in another file

In the awk below I am trying to add a penalty to a score to each matching $1 in file2 based on the sum of $3+$4 (variable TL) from file1. Then the $4 value in file1 is divided by TL and multiplied by 100 (this valvue is variable S). Finally, $2 in file2 - S gives the updated $2 result in file2.... (2 Replies)
Discussion started by: cmccabe
2 Replies

10. UNIX for Beginners Questions & Answers

awk match two fields in two files

Hi, I have two TEST files t.xyz and a.xyz which have three columns each. a.xyz have more rows than t.xyz. I will like to output rows at which $1 and $2 of t.xyz match $1 and $2 of a.xyz. Total number of output rows should be equal to that of t.xyz. It works fine, but when I apply it to large... (6 Replies)
Discussion started by: geomarine
6 Replies
__gnu_pbds(3cxx)														  __gnu_pbds(3cxx)

NAME
__gnu_pbds - SYNOPSIS
Classes struct associative_container_tag Basic associative-container. class basic_hash_table An abstract basic hash-based associative container. struct basic_hash_tag Basic hash. class basic_tree An abstract basic tree-like (tree, trie) associative container. struct basic_tree_tag Basic tree. struct binary_heap_tag Binary-heap (array-based). struct binomial_heap_tag Binomial-heap. class cc_hash_table A concrete collision-chaining hash-based associative container. struct cc_hash_tag Collision-chaining hash. class container_base An abstract basic associative container. struct container_tag Base data structure tag. struct container_traits container_traits class gp_hash_table A concrete general-probing hash-based associative container. struct gp_hash_tag General-probing hash. class list_update A list-update based associative container. struct list_update_tag List-update. struct null_mapped_type A mapped-policy indicating that an associative container is a set. struct ov_tree_tag Ordered-vector tree. struct pairing_heap_tag Pairing-heap. struct pat_trie_tag PATRICIA trie. struct priority_queue_tag Basic priority-queue. struct rb_tree_tag Red-black tree. struct rc_binomial_heap_tag Redundant-counter binomial-heap. struct sequence_tag Basic sequence. struct splay_tree_tag Splay tree. struct string_tag Basic string container, inclusive of strings, ropes, etc. struct thin_heap_tag Thin heap. class tree A concrete basic tree-based associative container. struct tree_tag tree. class trie A concrete basic trie-based associative container. struct trie_tag trie. Typedefs typedef void trivial_iterator_difference_type Functions void __throw_container_error (void) void __throw_insert_error (void) void __throw_join_error (void) void __throw_resize_error (void) Detailed Description GNU extensions for policy-based data structures for public use. Author Generated automatically by Doxygen for libstdc++ from the source code. libstdc++ Tue Nov 27 2012 __gnu_pbds(3cxx)
All times are GMT -4. The time now is 12:56 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy