Sort and tsort file sorting in bash
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML xmlns:fo = "http://www.w3.org/1999/XSL/Format"><HEAD><TITLE>Tip: Sorting files with sort and tsort</TITLE> <META http-equiv=Content-Type content="text/html; charset=UTF-8"> <META http-equiv=PICS-Label content='(PICS-1.1 "http://www.icra.org/ratingsv02.html" l gen true r (cz 1 lz 1 nz 1 oz 1 vz 1) "http://www.rsac.org/ratingsv01.html" l gen true r (n 0 s 0 v 0 l 0) "http://www.classify.org/safesurf/" l gen true r (SS~~000 1))'> <META http-equiv=Expires content=0> <META content="text utilities textutils GNU command commands command-line tool tools ptx tr uniq fold expand fmt pr wc checksum checksums cksum sum od comm diff headtail cut split less more cat sort tsort nl jacek artymiak IBM developerWorks tips hints" name=keywords> <META content="Save time and headaches by using sort and tsort -- instead of resorting to more complex solutions utilizing Perl or Awk." name=ABSTRACT> <META content="Save time and headaches by using sort and tsort -- instead of resorting to more complex solutions utilizing Perl or Awk." name=DESCRIPTION> <META content="Tip: Sorting files with sort and tsort" name=TITLE> <META content=https://www-136.ibm.com/developerworks/secure/feedback.jsp?domain=developerworks name=OWNER> <META scheme=iso8601 content=2003-3-6 name=DC.Date> <META scheme=rfc1766 content=en-us name=DC.Language> <META content=zz name=IBM.Country> <META content=Public name=SECURITY> <META content="Copyright (c) 2003 by IBM Corporation" name=DC.Rights> <META content=index,follow name=ROBOTS> <SCRIPT language=JavaScript src="Sorting files with sort and tsort_files/style.js" type=text/javascript></SCRIPT>
<SCRIPT language=JavaScript src="Sorting files with sort and tsort_files/spinbox.js" type=text/javascript></SCRIPT>
<SCRIPT language=JavaScript type=text/javascript>var title = "Tip: Sorting files with sort and tsort"; </SCRIPT>
<SCRIPT language=JavaScript type=text/javascript>var forumURL = ""; </SCRIPT>
<SCRIPT language=JavaScript type=text/javascript>var contentAreaList = "linux"; </SCRIPT>
<SCRIPT language=JavaScript src="Sorting files with sort and tsort_files/forumwindow.js" type=text/javascript></SCRIPT>
<SCRIPT language=JavaScript type=text/javascript>var emailAbstract = "Save time and headaches by using sort and tsort -- instead of resorting to more complex solutions utilizing Perl or Awk."; </SCRIPT>
<SCRIPT language=JavaScript src="Sorting files with sort and tsort_files/grabtitle.js" type=text/javascript></SCRIPT>
<SCRIPT language=JavaScript src="Sorting files with sort and tsort_files/emailfriend2.js" type=text/javascript></SCRIPT>
<SCRIPT language=JavaScript type=text/javascript>var demoURL = ""; </SCRIPT>
<SCRIPT language=JavaScript src="Sorting files with sort and tsort_files/demowindow.js" type=text/javascript></SCRIPT>
<META content="MSHTML 6.00.2800.1276" name=GENERATOR></HEAD> <BODY bgColor=#ffffff leftMargin=2 topMargin=2 marginheight="2" marginwidth="2">
<TBODY> </FORM> </TBODY><A
href="http://www-106.ibm.com/developerworks/cgi-bin/click.cgi?url=http://www.ibm.com/&origin=dwheader"><IMG height=47 alt=IBM src="Sorting files with sort and tsort_files/ibm-logo.gif" width=160border=0></A> |
<A
href="http://www-106.ibm.com/developerworks/linux/library/l-tiptex4.html#main"><IMG height=1 alt="Skip to main content" src="Sorting files with sort and tsort_files/c.gif" width=70border=0></A> |
<FORM id=form1 name=form1
action=http://www-106.ibm.com/search/searchResults.jsp method=get><INPUT
type=hidden value=1 name=searchType> <INPUT type=hidden value=dW
name=searchSite>
<TBODY>
</TBODY>
| ||||||||||||||||||||||||||||||
<A
class=mainlink href="http://www-106.ibm.com/developerworks/cgi-bin/click.cgi?url=http://www.ibm.com/&origin=dwheader">IBM home</A> | <A class=mainlink href="http://www-106.ibm.com/developerworks/cgi-bin/click.cgi?url=http://www.ibm.com/products/&origin=dwheader">Products & services</A> | <A class=mainlink href="http://www-106.ibm.com/developerworks/cgi-bin/click.cgi?url=http://www.ibm.com/support/">Support & downloads</A> | <A class=mainlink href="http://www-106.ibm.com/developerworks/cgi-bin/click.cgi?url=http://www.ibm.com/account/&origin=dwheader">Myaccount</A> |
<IMG height=1 alt="" src="Sorting files with sort and tsort_files/c.gif" width=5 border=0> |
<TBODY>
</TBODY>
|
<A name=main></A><A
name=top></A>
<TBODY> </TBODY><IMG height=15 alt=""
src="Sorting files with sort and tsort_files/c.gif" width=5border=0> | ||||
<IMG height=1 alt="" src="Sorting files with sort and tsort_files/c.gif" width=2 border=0> | Tip: Sorting files with sort and tsort | <IMG height=1 alt="" src="Sorting files with sort and tsort_files/c.gif" width=8 border=0> | <IMG height=1 alt=""
src="Sorting files with sort and tsort_files/c.gif" width=180 border=0>border=0></A></NOBR> |
<IMG height=1 alt=""
src="Sorting files with sort and tsort_files/c.gif" width=6border=0> |
<IMG height=1 alt=""
src="Sorting files with sort and tsort_files/c.gif" width=100border=0> | ||||
<IMG height=8 alt=""
src="Sorting files with sort and tsort_files/c.gif" width=100border=0> |
<IMG height=1 alt="" src="Sorting files with sort and tsort_files/c.gif" width=5 border=0> |
<TBODY>
</TBODY>
<TBODY> </TBODY>
<A
href="http://www-106.ibm.com/developerworks/linux/library/l-tiptex4.html#author1"><NAME>Jacek
Artymiak</NAME></A> (<A
href="mailto:jacek@artymiak.com?cc=&subject=Tip: Sorting files with sort and tsort">mailto:jacek@artymiak.com?cc=&subject=Tip:
Sorting files with sort and tsort</A>)
Although it is possible to write advanced sorting applications in Perl
or Awk, doing so may not always be necessary -- and is often a pain. Most
things you'll ever need are equally possible -- and lots easier -- with
the So, if you want to sort your password file, you could just use the following. (Note that you cannot send the output straight to the input file, because it will corrupt the input file. That's why you need to send it to a temporary file and then rename that file to /etc/passwd -- as shown below.) <A name=code1>Listing 1. Simple sort</A><TBODY> </TBODY>
<TBODY> </TBODY>
Should you want to reverse the order of sorting, use the
A very practical feature of Also, note that a whitespace transition is the default separator -- so
if fields are already separated by blank characters, you may omit the
separator and use For even finer control, you can use keys and offsets. Offsets are
separated from keys with a dot, as in Another option to watch out for is Further modifications of the sorting algorithm are possible with these
options: As an example of the use of a key number, consider:
This will sort the passwd file by group ID and within groups by userid, backwards. But that's not all that The last group of options deals with input, output, and temporary
files. For example, the Also important when you are processing large files is the
You can use
Or, you could use this command:
There is one condition in the second case: each input file must be
sorted before they are all sent to If you need a more esoteric kind of sort routine, you might want to
check out the
Of course, that isn't a very useful demonstration of what you'd use
will produce the output <TBODY> </TBODY>
Questions or comments? I'd love to hear from you -- send mail to <A href="mailto:jacek@artymiak.com">jacek@artymiak.com</A>. Next time, we'll delve into <A name=resources>Resources</A>
<IMG height=10 alt="" src="Sorting files with sort and tsort_files/c.gif" width=100 border=0>
<TBODY> </TBODY>
|
<IMG height=1 alt=""
src="Sorting files with sort and tsort_files/c.gif" width=1border=0> |
<IMG height=1 alt="" src="Sorting files with sort and tsort_files/c.gif" width=1> |
<A class=mainlink
href="http://www-106.ibm.com/developerworks/cgi-bin/click.cgi?url=http://www.ibm.com/ibm/?origin=dwheader">About IBM</A> | <A class=mainlink href="http://www-106.ibm.com/developerworks/cgi-bin/click.cgi?url=http://www.ibm.com/privacy/?origin=dwheader">Privacy</A> | <A class=mainlink href="http://www-106.ibm.com/developerworks/cgi-bin/click.cgi?url=http://www.ibm.com/legal/?origin=dwheader">Terms of use</A> | <A class=mainlinkhref="http://www-106.ibm.com/developerworks/cgi-bin/click.cgi?url=http://www.ibm.com/contact/?origin=dwheader">Contact</A> |
<SCRIPT language=JavaScript1.2 src="Sorting files with sort and tsort_files/stats.js" type=text/javascript></SCRIPT> <NOSCRIPT><IMG height=1 alt="" src="V:\knowledgebase\Linux\Sorting files with sort and tsort_files\c(1).gif" width=1 border=0></NOSCRIPT> </BODY></HTML>