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