The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 07-17-2008
jkl_jkl jkl_jkl is offline
Registered User
  
 

Join Date: Aug 2007
Posts: 46
awk help

Hi all,


Code:
Input file: file.txt
_____________________

7789 0
3445 1
7789 1
1223 0
4567 0
7789 0
7789 0
1212 1
3445 0
7789 1
3445 1


How can I get the output as:

<number> <no_0_entries> <no_1_entries>

i.e.

7789 3 2
3445 1 2
1223 1 0
4567 1 0
1212 0 1

I have no luck in solving this, please help . Thank you.
  #2 (permalink)  
Old 07-17-2008
Annihilannic Annihilannic is offline Forum Advisor  
  
 

Join Date: May 2008
Location: Sydney, Australia
Posts: 1,009
What have you tried?
  #3 (permalink)  
Old 07-17-2008
jkl_jkl jkl_jkl is offline
Registered User
  
 

Join Date: Aug 2007
Posts: 46
I tried upto this


Code:
$ awk '$2==0' file.txt > file.txt.0
$ awk '$2==1' file.txt > file.txt.1

$ cat file.txt.0
7789 0
1223 0
4567 0
7789 0
7789 0
3445 0

$ cat file.txt.1
3445 1
7789 1
1212 1
7789 1
3445 1

<0 entries>
$ awk '{count[$1]++}END{for(j in count) print j,count[j]}' file.txt.0
1223 1
4567 1
7789 3
3445 1

<1 entries>
$ awk '{count[$1]++}END{for(j in count) print j,count[j]}' file.txt.1
7789 2
3445 2
1212 1

  #4 (permalink)  
Old 07-17-2008
jkl_jkl jkl_jkl is offline
Registered User
  
 

Join Date: Aug 2007
Posts: 46
Could anyone help me on this please. Thank you.
  #5 (permalink)  
Old 07-17-2008
jkl_jkl jkl_jkl is offline
Registered User
  
 

Join Date: Aug 2007
Posts: 46
How to achieve the req output, please guide.
  #6 (permalink)  
Old 07-17-2008
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,557
u can try creating array with $1 and $2 as the key.
  #7 (permalink)  
Old 07-17-2008
aigles's Avatar
aigles aigles is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,433
A possible solution with awk :

Code:
awk '
{
   numbers[$1]++;
   zero[$1] += ($2 == 0 ? 1 : 0);
    one[$1] += ($2 == 1 ? 1 : 0);
}
END {
   for (i in numbers) {
      print i,zero[i],one[i]
   }
}' inputfile

Jean-Pierre.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 05:47 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0