Re: uniq without sort <-------------- GURU NEEDED
am 25.01.2008 18:00:54 von Edward Rosten
On Jan 25, 3:35 am, William James wrote:
> On Jan 25, 1:50 am, Thomas Troeger
> > cat somefile | awk '{ if (!h[$0]) { print $0; h[$0]=1 } }' > unique_lines
>
> Why are you using "cat"? Can't you guess that
> awk can read a file? Reading a file does not require
> magical powers.
This seems so crop up quite often. I often write pipelines like this
simply because it reads more logically left-to-right. It also makes
somefile more accessible when editing the line: it's very close to
where ^A will put the cursor. That's handy if you have a small or
irregular list of files to process.
Finally, there's no significant penalty for using cat: in some cases
the extra buffering can speed up certain processes.
My general rule of thumb is that it's worth writing something in the
more obvious and logical way (that depends on personal preference) if
there's no significant penalty, since my time is worth more to me than
my computer's time.
-Ed
--
(You can't go wrong with psycho-rats.)(http://mi.eng.cam.ac.uk/~er258)
/d{def}def/f{/Times s selectfont}d/s{11}d/r{roll}d f 2/m{moveto}d -1
r 230 350 m 0 1 179{ 1 index show 88 rotate 4 mul 0 rmoveto}for/s 12
d f pop 235 420 translate 0 0 moveto 1 2 scale show showpage
Re: uniq without sort <-------------- GURU NEEDED
am 25.01.2008 18:50:42 von Stephane CHAZELAS
On Fri, 25 Jan 2008 09:00:54 -0800 (PST), Edward Rosten wrote:
> On Jan 25, 3:35 am, William James wrote:
>> On Jan 25, 1:50 am, Thomas Troeger
>
>> > cat somefile | awk '{ if (!h[$0]) { print $0; h[$0]=1 } }' > unique_lines
>>
>> Why are you using "cat"? Can't you guess that
>> awk can read a file? Reading a file does not require
>> magical powers.
>
> This seems so crop up quite often. I often write pipelines like this
> simply because it reads more logically left-to-right. It also makes
> somefile more accessible when editing the line: it's very close to
> where ^A will put the cursor. That's handy if you have a small or
> irregular list of files to process.
>
> Finally, there's no significant penalty for using cat: in some cases
> the extra buffering can speed up certain processes.
>
> My general rule of thumb is that it's worth writing something in the
> more obvious and logical way (that depends on personal preference) if
> there's no significant penalty, since my time is worth more to me than
> my computer's time.
[...]
If you prefer it left to right, then you can write it:
< somefile awk '{...}'
which is perfectly legal in every Bourne-like, csh-like or
rc-like shell.
--
Stephane
Re: uniq without sort <-------------- GURU NEEDED
am 26.01.2008 13:55:09 von merlyn
>>>>> "Stephane" == Stephane Chazelas writes:
Stephane> If you prefer it left to right, then you can write it:
Stephane> < somefile awk '{...}'
Stephane> which is perfectly legal in every Bourne-like, csh-like or
Stephane> rc-like shell.
It's nice to see the "useless use of cat" crowd still acting on
advise I was giving out a decade ago.
:-)
--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!