How to add/sum same contents in a unix file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to add/sum same contents in a unix file
# 1  
Old 05-31-2011
How to add/sum same contents in a unix file

Input
Code:
1 2N 2N
1 2N 2N
1 2N 2N
1 2N Ateus
1 3 Mobiles 3M-100
1 3 Mobiles 3M-100
1 3 Mobiles 3M-100
1 3GNET 3gnet
1 3GNET 3gnet
1 3GNET G2828
1 3GNET G2828
1 3GNET G2829
1 3GNET G2829
1 3GNET G2829



OutPut should be
Code:
3 2N 2N
3 3 Mobiles 3M-100
2 3GNET 3gnet
2 3GNET G2828
3 3GNET G2829

Moderator's Comments:
Mod Comment
Please use code tags when posting data and code samples!

Last edited by vgersh99; 05-31-2011 at 04:25 PM.. Reason: please use code tags!
# 2  
Old 05-31-2011
Try:
Code:
awk '{sub ("[^ ]+ ","",$0);a[$0]++}END{for (i in a){print a[i],i}}' file

# 3  
Old 05-31-2011
Code:
awk 'BEGIN {
        while(getline)
        {
                N++;    L++;

                if((L>1)&&(PLINE != $0))
                {
                        TMP=PLINE
                        PLINE=$0
                        $0=TMP
                        $1=N
                        N=0
                        print $0;
                }
                else PLINE=$0
        }
}'

# 4  
Old 05-31-2011
Quote:
Originally Posted by bartus11
Try:
Code:
awk '{sub ("[^ ]+ ","",$0);a[$0]++}END{for (i in a){print a[i],i}}' file

Thanks , But it did not works, I run and got the following output.
2 2N 2N 7 2N 2N 1 2N Ateus 5 2N Ateus 3 3 Mobiles 3M-100 1 3 Mobiles OT401 1 3 Mobiles OT401 4 3 Mobiles OT401 1 3GNET 3gnet 4 3GNET 3gnet
# 5  
Old 06-01-2011
Code:
awk -F" " '{a[$2 " " $3 " " $4]++} END {for (i in a) {print a[i], i}}' file

Output:

3 3 Mobiles 3M-100
1 2N Ateus
3 2N 2N
2 3GNET G2828
3 3GNET G2829
2 3GNET 3gnet
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

UNIX utility to find difference in folder, file and contents of file against a base version

Hi, I am trying to find out whether there are any Unix utilities that compares folders, files and contents within the file and provides a comprehensive report. The comparison can be against base version of a folder and file with content. Can you please let me know of such a utility? Thanks,... (6 Replies)
Discussion started by: Sripathi_ks
6 Replies

2. UNIX for Dummies Questions & Answers

Write terminal contents into a one file in UNIX

Hi guys, How to write terminal contents into a file in Unix operating system Actually I created GUI by using Gtk2-perl. I want to display data on GUI whatever the contents writing on terminal. So which command I have to use and where that command to be run I mean in shell script or Perl... (2 Replies)
Discussion started by: kiran425
2 Replies

3. Shell Programming and Scripting

Folder contents getting appended as strings while redirecting file contents to a variable

Hi one of the output of the command is as below # sed -n "/CCM-ResourceHealthCheck:/,/---------/{/CCM-ResourceHealthCheck:/d;/---------/d;p;}" Automation.OutputZ$zoneCounter | sed 's/$/<br>/' Resource List : <br> *************************** 1. row ***************************<br> ... (2 Replies)
Discussion started by: vivek d r
2 Replies

4. Shell Programming and Scripting

copy contents of unix file to Word document

Hello, I have a unix file about 3000lines which i want to copy from and paste it into a Word document. If i cat the file and try to scroll through it then not everything is captured so i am getting and incomplete paste. Any help is really appreciated. jak (2 Replies)
Discussion started by: jakSun8
2 Replies

5. Shell Programming and Scripting

How to add the contents of a file?

Hi I have a file named Data cat Data 1797548295 2420652689 513068908 1426750759 3229436285 2710691077 i want to sum all these lines, can we use any direct command for that in unix. Thanks in advance.:b: (7 Replies)
Discussion started by: Prateek007
7 Replies

6. Shell Programming and Scripting

Removing the entire file contents using unix shell script.

I need to remove the entire file contents in file using the shell script. Actually the grap -v command will create one more file and it occupy the space also. I need to remove the entire file contents without creating new file using the shell scripting. Please help me. (5 Replies)
Discussion started by: praka
5 Replies

7. HP-UX

how to redirect the growing contents of log file to another file in unix

how to redirect the growing contents of log file to another file in unix (2 Replies)
Discussion started by: megh
2 Replies

8. UNIX for Dummies Questions & Answers

Deleting contents of a UNIX File

Hi ALL, Can anyone help me out. I have unix file, I need to delete the contents of the file. Can any one let me know the command to do this. The file has to be there, but the contents should be deleted. thanks Manas (6 Replies)
Discussion started by: manas6
6 Replies

9. Shell Programming and Scripting

Unix shell script to parse the contents of comma-separated file

Dear All, I have a comma-separated file. 1. The first line of the file(header) should have 4 commas(5 fields). 2. The last line of the file should have 1 comma(2 fields). Pls help me in checking this condition in a shell script. And the number of lines between the first line and last... (11 Replies)
Discussion started by: KrishnaSaran
11 Replies

10. UNIX for Dummies Questions & Answers

write a script & sum contents!PLEASE HELP

Hello everyone!! I have to write a script in unix that reads a file which is in the format: 1%blabla%30 2%blabla2%50 1%blabla3%40 2%blabla4%10 and produce the total quantities of 1 and 2: "1 = 70 2 = 60" I am taught basic unix commands, so I must keep it simple, how can i do this using... (7 Replies)
Discussion started by: questionmaker25
7 Replies
Login or Register to Ask a Question