Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place! |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
05-31-2012, 11:39 AM
|
#1
|
Member
Registered: Aug 2009
Posts: 349
Rep:
|
add a count to sort -u
I run this over a log file
Code:
casper@casperbox ~ $ more badthings20120531 | perl -nle 'print /^<br\s\/>(\w+)\s/' | sort -u
casper2011
casper2023
casper2030
casper2031
casper2032
casper2040
casper2041
casper2042
I sort uniqued them out -
Would there be a way to put a wc -l number near each instance to show how many times it showed up.
like it would be great to not only indicate what servers showed up in log file, but how many times they showed up in the log file.
Last edited by casperdaghost; 05-31-2012 at 11:41 AM.
|
|
|
05-31-2012, 01:02 PM
|
#2
|
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 23,231
|
I would solve it in perl, without more, sort and others.
Code:
#!/usr/bin/perl -w
use strict;
die if ! defined $ARGV[0];
my $file = $ARGV[0];
my %sum;
open (F, $file) or die "Error: cannot open file: $file, $!";
while (<F>) {
next if $_ !~ /^<br\s\/>(\w+)\s/;
$sum{$1}++;
}
close F or die "Error: cannot read file: $file, $!";
foreach (keys %sum) {
print "$_, $sum{$_}\n";
}
you should give the filename as argument to it
__________________________________
Happy with solution ... mark as SOLVED
If someone helps you, or you approve of what's posted, click the "Add to Reputation" button, on the left of the post.
Last edited by pan64; 05-31-2012 at 01:03 PM.
|
|
|
05-31-2012, 01:17 PM
|
#3
|
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,028
|
I could probably whip up something in awk or ruby but would need to see the format.
|
|
|
06-01-2012, 11:27 AM
|
#4
|
Member
Registered: Aug 2009
Posts: 349
Original Poster
Rep:
|
i use this alot
Code:
netstat -an | grep ESTABLISHED | awk '{print $5}' | awk -F: '{print $1}' | sort | uniq -c | awk '{ printf("%s\t%s\t",$2,$1) ; for (i = 0; i < $1; i++) {printf("*")}; print "" }'
i am having trouble applying it to this issue - I don't understand the "
awk '{ printf("%s\t%s\t",$2,$1)"
Code:
walt@walt-nasdaq:~$ cat /home/walt/noo | sed -e 's/pats/casper/g'
<br />casper0053 19:40:08 init: process \'/etc/rc.d/rc.start_java 2>&1 | logger\' (pid 532) exited. Scheduling for restart.
<br />casper0053 19:40:08 init: starting pid 1339, tty \'/dev/null\': \'/etc/rc.d/rc.start_java 2>&1 | logger\'
<br />casper0038 19:40:09 kernel: e1000e: eth1 NIC Link is Up 1000 Mbps Full Duplex, Flow Control: RX
<br />casper0054 19:40:09 init: process \'/etc/rc.d/rc.start_java 2>&1 | logger\' (pid 890) exited. Scheduling for restart.
<br />casper0054 netstat -an | grep ESTABLISHED | awk '{print $5}' | awk -F: '{print $1}' | sort | uniq -c | awk '{ printf("%s\t%s\t",$2,$1) ; for (i = 0; i < $1; i++) {printf("*")}; print "" }'
19:40:09 init: starting pid 1606, tty \'/dev/null\': \'/etc/rc.d/rc.start_java 2>&1 | logger\'
<br />casper0048 19:40:10 kernel: e1000e: eth1 NIC Link is Up 1000 Mbps Full Duplex, Flow Control: RX
<br />casper0055 19:40:11 init: process \'/etc/rc.d/rc.start_java 2>&1 | logger\' (pid 533) exited. Scheduling for restart.
<br />casper0055 19:40:11 init: starting pid 815, tty \'/dev/null\': \'/etc/rc.d/rc.start_java 2>&1 | logger\'
<br />casper0052 19:40:12 kernel: e1000e: eth1 NIC Link is Up 1000 Mbps Full Duplex, Flow Control: RX
<br />casper0059 19:40:12 init: process \'/etc/rc.d/rc.start_java 2>&1 | logger\' (pid 10090) exited. Scheduling for restart.
<br />casper0059 19:40:12 init: starting pid 10773, tty \'/dev/null\': \'/etc/rc.d/rc.start_java 2>&1 | logger\'
<br />casper0063 19:40:14 init: process \'/etc/rc.d/rc.start_java 2>&1 | logger\' (pid 23802) exited. Scheduling for restart.
<br />casper0063 19:40:14 init: starting pid 24329, tty \'/dev/null\': \'/etc/rc.d/rc.start_java 2>&1 | logger\'
<br />casper0053 19:40:14 kernel: e1000e: eth1 NIC Link is Up 1000 Mbps Full Duplex, Flow Control: RX
<br />casper0068 19:40:15 init: process \'/etc/rc.d/rc.start_java 2>&1 | logger\' (pid 3046) exited. Scheduling for restart.
<br />casper0068 19:40:15 init: starting pid 3583, tty \'/dev/null\': \'/etc/rc.d/rc.start_java 2>&1 | logger\'
<br />casper0054 19:40:16 kernel: e1000e: eth1 NIC Link is Up 1000 Mbps Full Duplex, Flow Control: RX
<br />casper0055 19:40:16 kernel: e1000e: eth1 NIC Link is Up 1000 Mbps Full Duplex, Flow Control: RX
<br />casper0070 19:40:17 init: process \'/etc/rc.d/rc.start_java 2>&1 | logger\' (pid 532) exited. Scheduling for restart.
<br />casper0070 19:40:17 init: starting pid 802, tty \'/dev/null\': \'/etc/rc.d/rc.start_java 2>&1 | logger\'
|
|
|
06-01-2012, 11:57 AM
|
#5
|
LQ Guru
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,326
|
|
|
|
06-01-2012, 12:05 PM
|
#6
|
Member
Registered: Aug 2009
Posts: 349
Original Poster
Rep:
|
oh snap schneidz - that does help - BIGTIME EASY
how can I sort by number and then just take the top ten avec bash?
|
|
|
06-01-2012, 01:36 PM
|
#7
|
LQ Guru
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,326
|
Quote:
Originally Posted by casperdaghost
oh snap schneidz - that does help - BIGTIME EASY
how can I sort by number and then just take the top ten avec bash?
|
Code:
sort | uniq -c | sort -n -r | head
worx, but theres gotta' be a more efficient way.
Last edited by schneidz; 06-01-2012 at 01:38 PM.
|
|
|
06-01-2012, 01:55 PM
|
#8
|
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,028
|
Well firstly I would say that grep and multiple awks are never the way to go:
Code:
netstat -an | awk 'BEGIN{FS="[ \t:]+";OFS="*"}/ESTAB/{a[$(NF-2)]++}END{asorti(a,b);while(++i < length(b)){$0="";$(a[b[i]]+1)="";print b[i]"\t"a[b[i]]"\t"$0}}'
I am not sure how the rest relates to your question though?
|
|
|
06-02-2012, 04:31 AM
|
#9
|
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 23,231
|
what about the perl script I gave you? This should work and also easily can be extended to sort and display the first ten:
Code:
#!/usr/bin/perl -w
use strict;
die if ! defined $ARGV[0];
my $file = $ARGV[0];
my %sum;
open (F, $file) or die "Error: cannot open file: $file, $!";
while (<F>) {
next if $_ !~ /^<br\s\/>(\w+)\s/;
$sum{$1}++;
}
close F or die "Error: cannot read file: $file, $!";
my $i = 0;
foreach ( sort { $sum{$b} <=> $sum{$a} } keys %sum )
{
print "$_, $sum{$_}\n";
last if (++$i > 4); # we print only the first 5 lines
}
You can use it as:
perlscript.pl filename > result
|
|
|
All times are GMT -5. The time now is 12:41 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|