Help with sum total number of record and total number of record problem asking


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with sum total number of record and total number of record problem asking
# 1  
Old 12-19-2011
Help with sum total number of record and total number of record problem asking

Input file
Code:
SFSQW	5192.56	
HNRNPK	611.486	
QEQW	1202.15	
ASDR	568.627	
QWET	6382.11	
SFSQW	4386.3	
HNRNPK	100
SFSQW	500

Desired output file
Code:
SFSQW	10078.86	3
QWET	6382.11	1	
QEQW	1202.15	1
HNRNPK	711.49	2
ASDR	568.63	1

The way I tried:
Code:
awk '{a[$1]+=$2}END{for (i in a){printf "%s\t%.2f\n",i,a[i]}}' input_file.txt  | sort -nrk2 > tmp.txt
awk '{print $1}' tmp.txt | sort | uniq -c | awk '{print $2"\t"$1}' > tmp2.txt
perl -e ' $col1=0; $col2=0; ($f1,$f2)=@ARGV; open(F2,$f2); while (<F2>) { s/\r?\n//; @F=split /\t/, $_; $line2{$F[$col2]} .= "$_\n" }; $count2 = $.; open(F1,$f1); while (<F1>) { s/\r?\n//; @F=split /\t/, $_; $x = $line2{$F[$col1]}; if ($x) { $num_changes = ($x =~ s/^/$_\t/gm); print $x; $merged += $num_changes } } '  tmp.txt tmp3.txt | awk '{print $1"\t"$2"\t"$4}' > desired_output.txt
SFSQW	10078.86	3
QWET	6382.11	1
QEQW	1202.15	1
HNRNPK	711.49	2
ASDR	568.63	1

# 2  
Old 12-19-2011
Try this:
Code:
awk '{a[$1]+=$2;b[$1]++}END{for(i in a)print i, a[i], b[i]}' file

This User Gave Thanks to Franklin52 For This Post:
# 3  
Old 12-19-2011
As per desired output.. with sum sorted in descending order.
Code:
perl -ane '$x{$F[0]}+=$F[1];$y{$F[0]}++; END{%x=reverse %x;print "$x{$_} $_ $y{$x{$_}}\n" for(reverse sort{$a<=>$b} keys %x)}' inputfile


Last edited by balajesuri; 12-19-2011 at 05:24 AM..
This User Gave Thanks to balajesuri For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Total record count of all the file present in a directory

Hi All , We need one help on the below requirement.We have multiple pipe delimited .txt file(around 100 .txt files) present on one directory.We need the total record count of all the files present in that directory without header.File format as below : ... (8 Replies)
Discussion started by: STCET22
8 Replies

2. Shell Programming and Scripting

Help with calculate the total sum of record in column one

Input file: 101M 10M10D20M1I70M 10M10D39M4I48M 10M10D91M 10M10I13M2I7M1I58M 10M10I15M1D66M Output file: 101M 101 0 0 10M10D20M1I70M 100 1 10 10M10D39M4I48M 97 4 10 10M10D91M 101 0 10 10M10I13M2I7M1I58M 88 13 0 10M10I15M1D66M 91 10 1 I'm interested to count how many total of... (6 Replies)
Discussion started by: perl_beginner
6 Replies

3. Shell Programming and Scripting

Problem facing to compare different column and print out record with smallest number

Hi, Input file 1 : 37170 37196 77 51 37174 37195 73 52 37174 37194 73 53 Desired Output file 1 : 37170 37196 77 51 Input file 2 : 37174 37195 73 0 37170 37196 77 0 Desired Output file 2 : 37174 37195 73 0 (1 Reply)
Discussion started by: cpp_beginner
1 Replies

4. Shell Programming and Scripting

Problem to print out record got smallest number in specific column

Hi, Anybody know how to print out the record that shown smallest number among column 3 and column 4 Case 1 Input : 37170 37196 77 51 37174 37195 73 52 37174 37194 73 53 Case 1 Output : 37170 37196 77 51 Case 2 Input : 469613 469660 73 ... (4 Replies)
Discussion started by: cpp_beginner
4 Replies

5. Linux

get total number of packages installed?

Hi, Which command would tell me of how many total number packages installed on my Redhat? Thank you, Lam (2 Replies)
Discussion started by: lamoul
2 Replies

6. HP-UX

Total number of files in a FS

Hello people, On HP-UX B.11.11 U 9000/800 How can I have in aprox. the total number of files in a specific FS? Is the number of used inodes a rough estimation of my total number of files? Server1 /Data:df -i . /Data (/dev/vg_Data/lvol1 ) : 18292960 total i-nodes 15800945 free... (3 Replies)
Discussion started by: drbiloukos
3 Replies

7. Shell Programming and Scripting

Help with calculate total sum of same data problem

Long list of input file: AGDRE1 0.1005449050 AGDRE1 2.1005443435 AGDRE1 1.2005449050 AGDRE1 5.1005487870 AASFV3 50.456304789 AASFV3 2.3659706549 AASFV3 6.3489807860 AASFV3 3.0089890148 RTRTRS 5.6546403546 . . Desired output file: AGDRE1 8.5021829410 AASFV3 62.180245240... (2 Replies)
Discussion started by: perl_beginner
2 Replies

8. Shell Programming and Scripting

csv file - adding total to a trailer record

Hi, I have a script which creates and modifies a csv file. I have managed to do everything I need to do apart from 1 thing. I need to append a trailer record to the file. I need this line to hold the total of an entire column of the csv file (skipping the 1st line which is a header). Can... (2 Replies)
Discussion started by: mcclunyboy
2 Replies

9. Shell Programming and Scripting

total number of lines in a file

Hi , How about find the total number of lines in a file ? How can i do that with the "grep" command ? (9 Replies)
Discussion started by: Raynon
9 Replies

10. Shell Programming and Scripting

total number of lines

Hi have following file |abcd 2|abcd |sdfh |sdfj I want to find total number of files haivng nothing in feild 1 using awk will command awk -F "|" '( $1=="") {print NR}' test_awk will work??? (4 Replies)
Discussion started by: mahabunta
4 Replies
Login or Register to Ask a Question