Count help - newbie


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Count help - newbie
# 8  
Old 03-20-2006
sorry I don't have time to understand your code.
I've changed mine for the 'diff' logic':

Code:
BEGIN {
  FS=OFS="|"
}

{
  if ( int($NF) >= 600 ) next

  idx= $1 SUBSEP $2
  NF--; $1 = $1
  if (!(idx in arr))
     arr[ idx ] = $0
  cnt [idx]++
}
END {
  for ( i in arr )
    print arr[i], cnt[i]
}

# 9  
Old 03-21-2006
Sorry Britney - didn't read the question properly Smilie Smilie Smilie
I can't improve on the awk solutions you've already had. Your requirement is a bit too subtle for the usual solutions for this sort of problem using sort/uniq etc and a straight ksh script (looping through, comparing keys and counting duplicates then printing on change of key) works fine but is pretty inelegant.

cheers
# 10  
Old 03-21-2006
Sorry, newbir writing code Smilie
vgersh99, your code work great but it show array

for ( i in arr )
print arr[i], cnt[i]

That's mean all field then comma count
what if I need to rearrange like add count in 3rd field and remove diff=$6 from your code how do i do it ? I am sure you will not save to another file and do awk again to rearange it .
Thanks,
# 11  
Old 03-21-2006
Quote:
Originally Posted by britney
Sorry, newbir writing code Smilie
vgersh99, your code work great but it show array

for ( i in arr )
print arr[i], cnt[i]

That's mean all field then comma count
what if I need to rearrange like add count in 3rd field and remove diff=$6 from your code how do i do it ? I am sure you will not save to another file and do awk again to rearange it .
Thanks,
adding the 'count' in the THIRD field:
Code:
BEGIN {
  FS=OFS="|"
  FLDinsert="3"
}

{
  idx= $1 SUBSEP $2
  NF--; $1 = $1
  if (!(idx in arr))
     arr[ idx ] = $0
  cnt [idx]++
}
END {
  for ( i in arr ) {
    n=split(arr[i], tmpA, OFS)
    tmpA[FLDinsert] = cnt[i] OFS tmpA[FLDinsert]
    for(iter=1; iter <= n; iter++)
        printf("%s%s", tmpA[iter], (iter < n) ? OFS : "\n")
  }
}

# 12  
Old 03-21-2006
wow .. thanks alot, I need to study more and read more to understand .
Thanks again
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to find the count of IP addresses that belong to different subnets and display the count?

Hi, I have a file with a list of bunch of IP addresses from different VLAN's . I am trying to find the list the number of each vlan occurence in the output Here is how my file looks like 1.1.1.1 1.1.1.2 1.1.1.3 1.1.2.1 1.1.2.2 1.1.3.1 1.1.3.2 1.1.3.3 1.1.3.4 So what I am trying... (2 Replies)
Discussion started by: new2prog
2 Replies

2. Shell Programming and Scripting

Compare file1 header count with file2 line count

What I'm trying to accomplish. I receive a Header and Detail file for daily processing. The detail file comes first which holds data, the header is a receipt of the detail file and has the detail files record count. Before processing the detail file I would like to put a wrapper around another... (4 Replies)
Discussion started by: pone2332
4 Replies

3. Shell Programming and Scripting

awk - count character count of fields

Hello All, I got a requirement when I was working with a file. Say the file has unloads of data from a table in the form 1|121|asda|434|thesi|2012|05|24| 1|343|unit|09|best|2012|11|5| I was put into a scenario where I need the field count in all the lines in that file. It was simply... (6 Replies)
Discussion started by: PikK45
6 Replies

4. Shell Programming and Scripting

count identical strings print last row and count

I have a sorted file like: Apple 3 Apple 5 Apple 8 Banana 2 Banana 3 Grape 31 Orange 7 Orange 13 I'd like to search $1 and if $1 is not the same as $1 in the previous row print that row and print the number of times $1 was found. so the output would look like: Apple 8 3 Banana... (2 Replies)
Discussion started by: dcfargo
2 Replies

5. Shell Programming and Scripting

perl newbie . &&..programming newbie

Hi, I am new to programming and also to perl..But i know 'perl' can come to my rescue, But I am stuck at many places and need help..any small help is much appreciated... below is the description of what i intend to acheive with my script. I have a files named in this format... (13 Replies)
Discussion started by: xytiz
13 Replies

6. Shell Programming and Scripting

perl newbie . &&..programming newbie (question 2)

Hello everyone, I am having to do a lot of perl scripting these days and I am learning a lot. I have this problem I want to move files from a folder and all its sub folders to one parent folder, they are all .gz files.. there is folder1\folder2\*.gz and there are about 50 folders... (1 Reply)
Discussion started by: xytiz
1 Replies

7. UNIX for Dummies Questions & Answers

UNIX newbie NEWBIE question!

Hello everyone, Just started UNIX today! In our school we use solaris. I just want to know how do I setup Solaris 10 not the GUI one, the one where you have to type the commands like ECHO, ls, pwd, etc... I have windows xp and I also have vmware. I hope I am not missing anything! :p (4 Replies)
Discussion started by: Hanamachi
4 Replies

8. Shell Programming and Scripting

Getting Sum, Count and Distinct Count of a file

Hi all this is a UNIX question. I have a large flat file with millions of records. col1|col2|col3 1|a|b 2|c|d 3|e|f 3|g|h footer**** I am supposed to calculate the sum of col1 1+2+3+3=9, count of col1 1,2,3,3=4, and distinct count of col1 1,2,3=c3 I would like it if you avoid... (4 Replies)
Discussion started by: singhabhijit
4 Replies

9. Shell Programming and Scripting

help a newbie count!

hello, I would really appreciate any help with this! i have files in the format ####_*.dat so like 2913_dsssf_sdfsdf_sddsf.dat 3208889423_lk3mnf_sdfe.dat what im trying to do is extract the number from the start of the file (end of number will always be followed by _ ) and copy the file... (3 Replies)
Discussion started by: jayydee
3 Replies

10. UNIX for Dummies Questions & Answers

How to count the record count in an EBCDIC file.

How do I get the record count in an EBCDIC file on a Linux Box. :confused: (1 Reply)
Discussion started by: oracle8
1 Replies
Login or Register to Ask a Question