LiveFire Labs - Online UNIX Training - Company Logo


Online UNIX Training with Hands-on Internet Lab


"Eliminate the expense and inconvenience of classroom training without eliminating the classroom experience."

 


Home
Internet Lab
Console Access
Sample Course

Student Login


LiveFire Labs' UNIX Tip, Trick, or Shell Script of the Week - View


Questions? Call
1.888.843.1637 or send us email

July 28, 2003 - Eliminating Duplicate Lines - The uniq Command

Consider this week's contents of unixfile, our frequently-used sample data file:

unix commands
shell script
command prompt
unix commands
unix system administration
shell script
unix commands
Since there are only a few number of lines in unixfile, duplicate lines can be quickly identified with a visual inspection and then removed if desired.  This manual process is not a viable option for files containing hundreds or thousands of lines.  The tool for a job of this magnitude is the uniq (unique) command:

uniq [option] [input-file] [output-file]

uniq can be used to display, count, or delete adjacent duplicate lines from a file or standard input (stdin).  If duplicate lines in a file are not adjacent to one another, uniq will not treat them as duplicates:

$ uniq unixfile
unix commands
shell script
command prompt
unix commands
unix system administration
shell script
unix commands

For this reason, uniq is often combined with the sort command to group duplicate lines prior to performing an action on them:

$ sort unixfile | uniq
command prompt
shell script
unix commands
unix system administration
To compress this combination of commands, the -u option can be used with sort to produce the same results:

$ sort -u unixfile
command prompt
shell script
unix commands
unix system administration

Two useful options for the uniq command are -d and -c.  I will leave it to you to discover what these options will do to your data.  Make sure to sort your data prior to using them...
 

Learn more...
  

If you are new to the UNIX or Linux operating system and would like to learn more, you may want to consider registering for LiveFire Labs' UNIX and Linux Operating System Fundamentals online training course.

If you already have a solid grasp of the fundamentals but would like to learn more about the Korn shell and basic and advanced shell scripting, taking our Korn Shell Scripting course will be beneficial to you.

Our innovative hands-on training model allows you to learn UNIX by completing hands-on exercises on real servers in our Internet Lab.


More Tips...

· 
Popular UNIX Tips from the Past

spacer Box Border
 

Receive the UNIX Tip, Trick, or Shell Script of the Week by Email


First Name:


Email Address:






   1.888.843.1637

Home - Contact us - Company info - Privacy Statement   

 
©2002-2003 LiveFire Labs.  All rights reserved.
Linux® is a registered trademark of Linus Torvalds, author and developer of this public domain operating system.
UNIX® is a registered trademark of The Open Group in the United States and other countries.