awk, help me - counting items and listing them


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk, help me - counting items and listing them
# 1  
Old 09-15-2011
Power awk, help me - counting items and listing them

This is my first ever post... please help! Smilie

I have two columns....here is part of the file...
Code:
12,                                             46798                                   
6692,                                          46799                                   
5710,                                          46799                                   
12,                                             46799                                   
6692,                                          46800                                   
5710,                                          46800                                   
12,                                             46800                                   
12,                                             46805

I want to know the number of times that the value in column 2 appears as well as the corresponding values in column 1

So I'm trying to get this....
Code:
Col2,          No of times it Occurs,      Col1 values for col2
46798,        1,                    12
46799,        3,                    6692, 5710, 12                
46800,        3,                    6692, 5710, 12   
46805,        1,                    12

Many many thanks for any help...


Moderator's Comments:
Mod Comment Please use [code] and [/code] tags when posting code, data or logs etc. to preserve formatting and enhance readability, thanks. Some less comic-like speech in the subject please, thanks. See PM for code tags.

Last edited by zaxxon; 09-15-2011 at 09:12 AM.. Reason: code tags
# 2  
Old 09-15-2011
Code:
for i in `awk -F, '{print $2|"uniq"}' infile`
do
echo "$i `grep -c $i infile`"
grep $i infile | awk '{print $1|"xargs"}'
done | paste - -

# 3  
Old 09-15-2011
Code:
$> cat mach.ksh
awk '
     BEGIN{
          printf("%-10s%-10s%-30s\n","Col2","Count","Values")
     }
     {
          gsub(/,/,"",$1)
          a[$2]++
          b[$2]?b[$2]=b[$2] x $1:b[$2]=$1
     }
     END{
          for(x in a){
               printf("%-10s%-10s%-30s\n",x",",a[x]",",b[x])
          }
     }
' x=", " infile

Output:
Code:
$> ./mach.ksh
Col2      Count     Values
46798,    1,        12
46799,    3,        6692, 5710, 12
46800,    3,        6692, 5710, 12
46805,    1,        12

# 4  
Old 09-15-2011
Code:
awk -F', +' 'END {
  for (k in v)
    print k, c[k], v[k]
  }
{
  v[$2] = $2 in v ? v[$2] ", " $1 : $1
  c[$2]++
   }' OFS=',\t' infile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Pass an array to awk to sequentially look for a list of items in a file

Hello, I need to collect some statistical results from a series of files that are being generated by other software. The files are tab delimited. There are 4 different sets of statistics in each file where there is a line indicating what the statistic set is, followed by 5 lines of values. It... (8 Replies)
Discussion started by: LMHmedchem
8 Replies

2. Shell Programming and Scripting

awk - function to return permutations of n items out of m

Hi, I'm trying to write an awk function that returns all possible permutations of n items chosen in a list of m items. For example, given the input "a,b,c,d,e" and 3, the function should return the following : a a a a a b a a c a b a a b b ... c a a c a b ... e e c e e d e e e (125... (21 Replies)
Discussion started by: cjnwl
21 Replies

3. UNIX for Dummies Questions & Answers

awk colon separated items

Hi, I need to filter my data based on items in column 23. Column 1 until column 23 are tab separated. This is how column 23 looks like: PRIMARY=<0/1:504:499,5:.:.:.:0.01:1:15:.> I want to extract lines if items 7 (separated by : ) in column 23 are more than 0.25 . In example above , item... (2 Replies)
Discussion started by: janshamsani
2 Replies

4. Shell Programming and Scripting

awk counting question

Probably a simple to this, but unsure how to do it. I would prefer an AWK solution. Below is the data set. 1 2 3 2 5 7 4 6 9 1 5 4 8 5 7 1 1 10 15 3 12 3 7 9 9 8 10 4 5 2 9 1 10 4 7 9 7 12 6 9 13 8 For the second... (11 Replies)
Discussion started by: mollydog11
11 Replies

5. Shell Programming and Scripting

Counting Fields with awk

ok, so a user can specify options as is shown below: ExA: cpu.pl!23!25!-allow or ExB: cpu.pl!23!25!-block!all options are delimited by the exclamation mark. now, in example A, there are 4 options provided by the user. in example B, there are 5 options provided by the user. ... (3 Replies)
Discussion started by: SkySmart
3 Replies

6. Shell Programming and Scripting

counting using awk

Hi, I want to perform a task using shell script. I am new to awk programming and any help would be greatly appreciated. I have the following 3 files (for example) file1: Name count Symbol chr1_1_50 10 XXXX chr3_101_150 30 YYYY File2: Name ... (13 Replies)
Discussion started by: Diya123
13 Replies

7. Shell Programming and Scripting

Counting the number of readable, writable, and executable items in a directory

Hello, I'm writing a script in sh in which the first command line argument is a directory. from that, i'm suppose to count the number of readable, writable, and executable items in the directory. I know using $1 represents the directory, and ls would display all the items in the directory, and that... (4 Replies)
Discussion started by: kratos22
4 Replies

8. Shell Programming and Scripting

Counting items in variables, How come this works, However

Hi All, To start with, I have been reading this site for years, Unfortunately I do not consider myself versed well enough with scripts to provide useful help to others. The Blind cannot lead the Blind! Many of you have provided me with brain food and solutions over the years without even... (4 Replies)
Discussion started by: Festus Hagen
4 Replies

9. Shell Programming and Scripting

Counting with Awk

I need "awk solution" for simple counting! File looks like: STUDENT GRADE student1 A student2 A student3 B student4 A student5 B Desired Output: GRADE No.of Students A 3 B 2 Thanks for awking! (4 Replies)
Discussion started by: saint2006
4 Replies

10. Shell Programming and Scripting

awk between items including items

OS=HP-UX ksh The following works, except I want to include the <start> and <end> in the output. awk -F '<start>' 'BEGIN{RS="<end>"; OFS="\n"; ORS=""} {print $2} somefile.log' The following work in bash but not in ksh sed -n '/^<start>/,/^<end>/{/LABEL$/!p}' somefile.log (4 Replies)
Discussion started by: Ikon
4 Replies
Login or Register to Ask a Question