search the largest number and duplicates string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting search the largest number and duplicates string
# 1  
Old 05-17-2007
search the largest number and duplicates string

Hi,

My input file contain list of username, and it may have name with number as a suffix (if duplicated).
Ex:
mary
john2
mike
john3
john5
mary10
alexa


So i want to check with a specific username (without suffix number) how many duplicated name, and what is the biggest number
Ex: with "john" -> ouput: 3 duplicates, biggest is 5
with "mary" -> ouput: 2 duplicates, biggest is 10

Anyone help?

Thanks
# 2  
Old 05-17-2007
Code:
awk ' { sub("[0-9]+$"," &"); arr[$1] = $2 > arr[$1] ? $2 : arr[$1]; cnt[$1]++; }
> END { for ( nm in cnt ) { if( cnt[nm] > 1 ) print nm , arr[nm] , "Count :" cnt[nm] } } ' filename

# 3  
Old 05-17-2007
I got this error:

awk: { sub("[0-9]+$"," &"); arr[$1] = $2 > arr[$1] ? $2 : arr[$1]; cnt[$1]++; } > END { for ( nm in cnt ) { if( cnt[nm] > 1 ) print nm , arr[nm] , "Count :" cnt[nm] } }
awk: ^ syntax error
# 4  
Old 05-17-2007
Quote:
Originally Posted by fongthai
I got this error:

awk: { sub("[0-9]+$"," &"); arr[$1] = $2 > arr[$1] ? $2 : arr[$1]; cnt[$1]++; } > END { for ( nm in cnt ) { if( cnt[nm] > 1 ) print nm , arr[nm] , "Count :" cnt[nm] } }
awk: ^ syntax error
Remove > before END in this code
Code:
awk ' { sub("[0-9]+$"," &"); arr[$1] = $2 > arr[$1] ? $2 : arr[$1]; cnt[$1]++; }
> END { for ( nm in cnt ) { if( cnt[nm] > 1 ) print nm , arr[nm] , "Count :" cnt[nm] } } ' filename

# 5  
Old 05-17-2007
Assuming your data is in a file 'data.txt', this perl will do it

Code:
$name = $ARGV[0];
$count=0;
$biggest=0;

open (DATA,"<data.txt");

while($line = <DATA>)
{
  chomp;
  if ($line =~ /$name(\d*)/)
  {
    $num = $1;
    $count++;
    $biggest = $num > $biggest?$num:$biggest;
  }
}

print "$ARGV[0]: $count duplicates, biggest is $biggest\n";

# 6  
Old 05-17-2007
Thanks anbu23 and Unbeliever,

Anbu,
I know the basic of awk, but your script is so hard to understand, could you please explain me? specically this part:

{ sub("[0-9]+$"," &"); arr[$1] = $2 > arr[$1] ? $2 : arr[$1]; cnt[$1]++; }
# 7  
Old 05-17-2007
Output display needs fine tuning.
Code:
awk '
{
   word=$1
   sub(/[a-zA-Z]+/,"",$1)
   num=$1
   sub(/[0-9]+/,"",word) 
   wordcount[word]++
   if  ( user[word] < num ) {
      user[word]=num
   }   
}
END {
    for (usr in user) {
       print usr "->\t" "largest number: " user[usr]
    }
    for (c in wordcount) {
       print c "->\t" wordcount[c],"duplicates"
    }    
}' "file"

output:
Code:
 # ./test.sh
mary->  largest number: 10
mike->  largest number:
john->  largest number: 5
alexa-> largest number:
mary->  2 duplicates
mike->  1 duplicates
john->  3 duplicates
alexa-> 1 duplicates

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search string within a file and list common words from the line having the search string

Hi, Need your help for this scripting issue I have. I am not really good at this, so seeking your help. I have a file looking similar to this: Hello, i am human and name=ABCD. How are you? Hello, i am human and name=PQRS. I am good. Hello, i am human and name=ABCD. Good bye. Hello, i... (12 Replies)
Discussion started by: royzlife
12 Replies

2. Shell Programming and Scripting

Locating the largest number and performing division

I have a tab delimited file with the following format 1 r 109 45 3 5 6 7 2 f 300 249 5 8 10 3 g 120 4 5 110 0 4 t 400 300 250 0 0 ..... ..... 100,000 lines I would like to get the largest number in columns 4, 5, 6, 7, 8 and divide that largest number with the number in column 3.... (4 Replies)
Discussion started by: Kanja
4 Replies

3. Shell Programming and Scripting

Help in printing n number of lines if a search string matches in a file

Hi I have below script which is used to grep specific errors and if error string matches send an email alert. Script is working fine , however , i wish to print next 10 lines of the string match to get the details of error in the email alert Current code:- #!/bin/bash tail -Fn0 --retry... (2 Replies)
Discussion started by: neha0785
2 Replies

4. Shell Programming and Scripting

Search several string and convert into a single line for each search string using awk command AIX?.

I need to search the file using strings "Request Type" , " Request Method" , "Response Type" and by using result set find the xml tags and convert into a single line?. below are the scenarios. Cat test Nov 10, 2012 5:17:53 AM INFO: Request Type Line 1.... (5 Replies)
Discussion started by: laknar
5 Replies

5. Shell Programming and Scripting

How to search number of occurrences of a particular string in a file through vi editor?

i have one file, i am doing 'vi Filename' now i want to search for particular string and i want to know how many times that string occurs in whole file (5 Replies)
Discussion started by: sheelsadan
5 Replies

6. Shell Programming and Scripting

Largest number in array. Help!

I need to calculate the biggest number in array size n. Example: Users enter: 1 7 4 9 The biggest number is : 9 Simple but I'm really new on this on Shell/Bash! Anything will be helpful! Thanks! #!/bin/bash printf "\tEnter a list of numbers, with spaces: " read -a ARRAY BIG=$1... (5 Replies)
Discussion started by: Sundown
5 Replies

7. Shell Programming and Scripting

Taking largest (negative) number from column of coordinates and adding positive form to every other

Hello all, I'm new to the forums and hope to be able to contribute something useful in the future; however I must admit that what has prompted me to join is the fact that currently I need help with something that has me at the end of my tether. I have a PDB (Protein Data Bank) file which I... (13 Replies)
Discussion started by: crunchgargoyle
13 Replies

8. UNIX for Dummies Questions & Answers

How to print largest and smallest number.

Hey. This is pretty easy stuff but I'm learning the basics of Unix at the moment so keep that in mind. I have to: 1) Write a C-shell script to monitor user activity on the server for 13 minutes. 2) Then print the smallest and largest number of users during these 13 minutes. I have this: 1)... (2 Replies)
Discussion started by: amp10388
2 Replies

9. Shell Programming and Scripting

checking the smallest and largest number

Hi All, My script is reading a log file line by line log file is like ; 19:40:22 :INFO Total time taken to Service External Request---115ms 19:40:25 DEBUG : Batch processed libdaemon.x86_64 0-0.10-5.el5 - u 19:40:22 INFO Total time taken to Service External Request---20ms 19:40:24... (4 Replies)
Discussion started by: subin_bala
4 Replies

10. UNIX for Dummies Questions & Answers

count the number of files which have a search string, but counting the file only once

I need to count the number of files which have a search string, but counting the file only once if search string is found. eg: File1: Please note that there are 2 occurances of "aaa" aaa bbb ccc aaa File2: Please note that there are 3 occurances of "aaa" aaa bbb ccc... (1 Reply)
Discussion started by: sudheshnaiyer
1 Replies
Login or Register to Ask a Question