The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Calling a perl script from a perl script new2ss Shell Programming and Scripting 6 05-24-2009 05:03 PM
Include PERL script with in the unix shell script ganapati UNIX for Dummies Questions & Answers 1 04-29-2008 12:18 PM
here document to automate perl script that call script hogger84 Shell Programming and Scripting 3 10-22-2007 10:15 AM
Modify Perl script to work with txt - Permissions script joangopan Shell Programming and Scripting 1 09-12-2007 11:38 PM
Perl: Run perl script in the current process vino Shell Programming and Scripting 10 12-09-2005 10:45 AM

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

Join Date: Apr 2008
Posts: 12
help on perl script..

Hi perl gurus,

The below lines are the 1st few lines of my logfile to scan.
Based on this, i need to get the TOP5 UserIDs having the most activities/transactions, and the top5 tasks as well.

For example, thomsonj (appearing 4x sofar) would be the top1 UserID, and WithdrawCash (4x) would be the top1 Task.

.....
.....
04:45:48 >> ==========START of MESSAGE==========
04:45:48 >> UserID: gibsonwe
04:45:48 >> Task: CheckBalance
04:45:48 >> Processing....
04:45:50 >> Response: Current balance is [$1]

07:30:27 >> ==========START of MESSAGE==========
07:30:27 >> UserID: thomsonj
07:30:27 >> Task: WithdrawCash
07:30:27 >> Processing....
07:30:27 >> Response: [$1] dispensed

10:18:09 >> ==========START of MESSAGE==========
10:18:09 >> UserID: greendav
10:18:09 >> Task: FundTransfer
10:18:10 >> Response: Successfully transferred [$1]

13:47:23 >> ==========START of MESSAGE==========
13:47:23 >> UserID: thomsonj
13:47:23 >> Task: WithdrawCash
13:47:23 >> Processing....
13:47:26 >> Response: [$1] dispensed

15:06:15 >> ==========START of MESSAGE==========
15:06:15 >> UserID: thomsonj
15:06:15 >> Task: CheckBalance
15:06:15 >> Processing....
15:06:15 >> Response: Current balance is [$1]

17:44:50 >> ==========START of MESSAGE==========
17:44:50 >> UserID: greendav
17:44:50 >> Task: BillPayment
17:44:50 >> Processing....
17:44:50 >> Response: Successfully paid [$1]

20:25:29 >> ==========START of MESSAGE==========
20:25:29 >> UserID: thomsonj
20:25:29 >> Task: WithdrawCash
20:25:29 >> Processing....
20:25:29 >> Response: [$1] dispensed
.....
.....


Any help would be greatly appreciated.
Thanks much.
  #2 (permalink)  
Old 07-15-2008
zedex zedex is offline
Registered User
  
 

Join Date: Feb 2007
Location: india,mumbai
Posts: 138
what u can do is use hash in perl
1. open file in read mode
2. use regex to get filed after "User Id:"
3. put this "user Id" as key in Hash if not present
4. every time you find user present increase key value by 1
5. at the end sort keys of hash and do print.

following link might help

Perl Hash Howto
  #3 (permalink)  
Old 07-15-2008
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,847
Giving a try:

Code:
perl -lne'
  $usr{$1}++ if /UserID: (\w+)/;
  $task{$1}++ if /Task: (\w+)/;
  if (eof) {
    $, = "\n";
    print "\nTop 5 users:\n", "=" x 12, 
      (sort { $usr{$b} <=> $usr{$a} } keys %usr)[0..4];
    print "\nTop 5 tasks:\n",  "=" x 12, 
      (sort { $task{$b} <=> $task{$a} } keys %task)[0..4]; 
    }' input
  #4 (permalink)  
Old 07-15-2008
xinoo xinoo is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 12
Thanks so much radoulov, it worked

But do u mind explaining a bit on the code.
Appreciate it mate.
  #5 (permalink)  
Old 07-16-2008
KevinADC KevinADC is offline Forum Advisor  
Registered User
  
 

Join Date: Jan 2008
Posts: 731
people should really do their own school/class/course work
  #6 (permalink)  
Old 07-16-2008
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,847
KevinADC is right,
I'm not sure if this is a homework (it may be) and this is not helpful (give vs teach a man a/to fish).
The interpretation of the code is left as an exercise to the OP.
  #7 (permalink)  
Old 07-16-2008
summer_cherry summer_cherry is offline Forum Advisor  
Registered User
  
 

Join Date: Jun 2007
Location: Beijing China
Posts: 1,078
Code:
sed 's/.*>>//' a > a.new
awk 'BEGIN{
FS=":"
un=0
tn=0}
{
if (index($1,"UserID")!=0)
	user[$2]++
if (index($1,"Task")!=0)
	task[$2]++
}
END{
	for (i in user)
		if(user[i]>un){un=user[i];u=i}
	for(j in task)
		if(task[j]>tn){tn=task[j];t=j}
	print u"  - "un
	print t"  - "tn
}' a.new
rm a.new
Closed Thread

Bookmarks

Tags
perl, perl regex, regex

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 10:42 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