1 |
On 2011-05-17, Neil Bothwick <neil@××××××××××.uk> wrote: |
2 |
> On Tue, 17 May 2011 01:33:39 +0200, Alan McKinnon wrote: |
3 |
> |
4 |
>> grep "GET /Tmp/Linux/G" | /var/log/apache2/access_log | grep-v <myip> | |
5 |
>> \ awk '{print $1}' | sort | uniq | wc |
6 |
>> |
7 |
>> In true grand Unix tradition you cannot get quicker, dirtier or more |
8 |
>> effective than that |
9 |
>> |
10 |
> |
11 |
> awk does pattern matching, o you can ditch the grep stage and use |
12 |
> |
13 |
> awk '! /myip/ {print $1}' |
14 |
> |
15 |
> You could use awk to search for the GET patterns too, not only saving yet |
16 |
> another process, but making sure that no one else, including you next |
17 |
> month, can work out what the command is supposed to do. |
18 |
> |
19 |
|
20 |
Meh, me forgetting what an awk snippet do? Never! |
21 |
|
22 |
sed ... now that's a wholly different story :-P |
23 |
|
24 |
> sort -u would save having a separate process for uniq, but I've no idea |
25 |
> if it's faster. It's only worth using sort -u if you would use uniq with |
26 |
> no arguments. |
27 |
> |
28 |
|
29 |
And you can actually do the 'uniq' or '-u' function within awk. Quite |
30 |
easily, in fact. |
31 |
|
32 |
Here's a sample of awk doing uniq: |
33 |
|
34 |
awk '!x[$1]++ { print $1 }' |
35 |
|
36 |
Benefit? It doesn't care if the non-unique lines are one-after-another |
37 |
or spread all over the text. The above snippet prints only the first |
38 |
occurence. Combine that with a test for match: |
39 |
|
40 |
awk '!x[$1]++ && $0 ~ /awesome_regex_pattern/ {print $1}' |
41 |
|
42 |
then with a test for negated match |
43 |
|
44 |
awk '!x[$1]++ && $0 ~ /awesome_regex_pattern/ && $0 !~ |
45 |
/more_awesome_regex/ {print $1}' |
46 |
|
47 |
Rgds, |
48 |
-- |
49 |
Pandu E Poluan - IT Optimizer |
50 |
My website: http://pandu.poluan.info/ |