On Tue, May 17, 2011 at 5:43 AM, Pandu Poluan <pa...@poluan.info> wrote:
> On 2011-05-17, Neil Bothwick <n...@digimed.co.uk> wrote:
>> On Tue, 17 May 2011 01:33:39 +0200, Alan McKinnon wrote:
>>
>>> grep "GET /Tmp/Linux/G" | /var/log/apache2/access_log | grep-v <myip> |
>>> \ awk '{print $1}' | sort | uniq | wc
>>>
>>> In true grand Unix tradition you cannot get quicker, dirtier or more
>>> effective than that
>>>
>>
>> awk does pattern matching, o you can ditch the grep stage and use
>>
>>  awk '! /myip/ {print $1}'
>>
>> You could use awk to search for the GET patterns too, not only saving yet
>> another process, but making sure that no one else, including you next
>> month, can work out what the command is supposed to do.
>>
>
> Meh, me forgetting what an awk snippet do? Never!
>
> sed ... now that's a wholly different story :-P
>
>> sort -u would save having a separate process for uniq, but I've no idea
>> if it's faster. It's only worth using sort -u if you would use uniq with
>> no arguments.
>>
>
> And you can actually do the 'uniq' or '-u' function within awk. Quite
> easily, in fact.
>
> Here's a sample of awk doing uniq:
>
> awk '!x[$1]++ { print $1 }'
>
> Benefit? It doesn't care if the non-unique lines are one-after-another
> or spread all over the text. The above snippet prints only the first
> occurence. Combine that with a test for match:
>
> awk '!x[$1]++ && $0 ~ /awesome_regex_pattern/ {print $1}'
>
> then with a test for negated match
>
> awk '!x[$1]++ && $0 ~ /awesome_regex_pattern/ && $0 !~
> /more_awesome_regex/ {print $1}'
>
> Rgds,
> --
> Pandu E Poluan - IT Optimizer
> My website: http://pandu.poluan.info/
>
>

I have always wondered if there is a way to do awk '{ print $1}' using
only builtin bash functions when you only have a one line string

Reply via email to