[Buffalo-pm] Unix Command / Bash Question...

Richard Dice rdice at pobox.com
Fri Jan 27 07:40:56 PST 2006


Daniel,

The unix 'uniq' command collapses *adjacent* identical lines into unique lines.
 If I was to have a file like

1
2
1
2
1
2

and I did a "cat FILE | uniq", the output would be 

1
2
1
2
1
2

The standard practice is to run sort and then uniq:

"cat FILE | sort | uniq" yields 

1
2

Some version of 'sort' have uniq built into it, per:

"cat FILE | sort -u" yielding

1
2

Your problem lies in the realm of this.  In the first case, there must be come
stuff in between the two '10.x.x.x' lines that you grepped for that wasn't in
the line, so the uniq wouldn't collapse these into a single line.  In the second
case, your grep removed those lines first, thus allowing uniq to do its job.

Cheers,
Richard

Quoting DANIEL MAGNUSZEWSKI <dmagnuszewski at mandtbank.com>:
> All,
> 
> I wrote a Perl script to parse a file for ip addresses and hostnames,
> and to print out in the format of:
> 
> $ip,$hostname
> 
> There are some duplicates in the list, so I was just using some unix
> commands and pipes to rid them from the output. While doing this, I ran
> into something interesting. 
> 
> If I do:
> 
> user at server:/opt/cwscripts# ./dcraudit.pl | uniq | grep -i '10.x.x.x'
> 
> I get the output of:
> 
> 10.x.x.x,router1
> 10.x.x.x,router1
> 
> If I switch the 'uniq' and 'grep' and run:
> 
> user at server:/opt/cwscripts# ./dcraudit.pl |  grep -i '10.x.x.x' | uniq
> 
> I get the output of:
> 
> 10.x.x.x,router1
> 
> What is the technical reason for this? I assumed that it would work
> either way.
> 
> Thanks.
> 
> -Dan
> 
> _______________________________________________
> Buffalo-pm mailing list
> Buffalo-pm at pm.org
> http://mail.pm.org/mailman/listinfo/buffalo-pm
> 




-------------------------------------------------
This mail sent through IMP: http://horde.org/imp/


More information about the Buffalo-pm mailing list