More tr examplestr ’&’ ’#’ translate ampersands to hashtr -s ’\t’ squeeze consecutive tabs to one tabUnix Tools: Shells, part 4More tr examples$ cat /etc/hosts# Do not remove the following line, or various programs# that require network functionality will fail.127.0.0.1 localhost.localdomain localhost128.186.120.8 sophie.cs.fsu.edu127.0.0.1 a.as-us.falkag.net127.0.0.1 clk.atdmt.com$ tr -s ’\t’ < /etc/hosts# Do not remove the following line, or various programs# that require network functionality will fail.127.0.0.1 localhost.localdomain localhost128.186.120.8 sophie.cs.fsu.edu127.0.0.1 a.as-us.falkag.net127.0.0.1 clk.atdmt.comUnix Tools: Shells, part 4More tr examplestr -d ’\015’ delete carriage returns from a DOS fileUnix Tools: Shells, part 4basenamebasename lets you remove leading directory strings. It can alsoremove suffixes simply by specifying the suffix as a second argument.$ basename ‘pwd‘2006-Fall$ var1=/etc/inetd.conf$ basename $var1 .confinetdUnix Tools: Shells, part 4dirnamedirname does the opposite function of basename: it returns theleading path components from a directory name.$ echo ‘pwd‘/mnt-tmp/Lexar/fsucs/cop-4342/2006-Fall$ dirname ‘pwd‘/mnt-tmp/Lexar/fsucs/cop-4342$ dirname 05-shells4.tex.$ dirname ‘pwd‘/xyz/mnt-tmp/Lexar/fsucs/cop-4342/2006-FallUnix Tools: Shells, part 4sortFor all of the files listed, sort will sort the concatenated lines ofthose files to stdout. The most useful options are -f, which means tofold case, -n to sort numerically rather alphabetically, -u to removeduplicates (“u” is short for “unique”), and -r to reverse the order ofthe sort.You can specify particular fields to sort by specifying a field separator(whitespace is the default) with the -t option, and then using -k tospecify particular fields.Unix Tools: Shells, part 4sort examples$ sort /etc/passwdadm:x:3:4:adm:/var/adm:/sbin/nologinamanda:x:33:6:Amanda user:/var/lib/amanda:/bin/bashapache:x:48:48:Apache:/var/www:/sbin/nologinbin:x:1:1:bin:/bin:/sbin/nologincanna:x:39:39:Canna Service User:/var/lib/canna:/sbin/nologindaemon:x:2:2:daemon:/sbin:/sbin/nologindesktop:x:80:80:desktop:/var/lib/menu/kde:/sbin/nologinUnix Tools: Shells, part 4sort examples$ sort -r /etc/passwdxfs:x:43:43:X Font Server:/etc/X11/fs:/sbin/nologinwnn:x:49:49:Wnn Input Server:/var/lib/wnn:/sbin/nologinwebalizer:x:67:67:Webalizer:/var/www/usage:/sbin/nologinvmail:x:502:502::/home/vmail:/sbin/nologinvcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologinuucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologinuser1:x:505:505::/home/user1:/bin/bashtest:x:503:503::/home/test:/sbin/nologinsync:x:5:0:sync:/sbin:/bin/syncUnix Tools: Shells, part 4sort examples$ sort -k3,3n -t: /etc/passwdroot:x:0:0:root:/root:/bin/bashbin:x:1:1:bin:/bin:/sbin/nologindaemon:x:2:2:daemon:/sbin:/sbin/nologinadm:x:3:4:adm:/var/adm:/sbin/nologinlp:x:4:7:lp:/var/spool/lpd:/sbin/nologinsync:x:5:0:sync:/sbin:/bin/syncshutdown:x:6:0:shutdown:/sbin:/sbin/shutdownhalt:x:7:0:halt:/sbin:/sbin/haltmail:x:8:12:mail:/var/spool/mail:/sbin/nologinUnix Tools: Shells, part 4sort examples$ sort -k4,4n -k3,3n -t: /etc/passwdroot:x:0:0:root:/root:/bin/bashsync:x:5:0:sync:/sbin:/bin/syncshutdown:x:6:0:shutdown:/sbin:/sbin/shutdownhalt:x:7:0:halt:/sbin:/sbin/haltoperator:x:11:0:operator:/root:/sbin/nologinbin:x:1:1:bin:/bin:/sbin/nologindaemon:x:2:2:daemon:/sbin:/sbin/nologinadm:x:3:4:adm:/var/adm:/sbin/nologinUnix Tools: Shells, part 4groff and gtblThere are a lot of great packages out there, such as graphviz.A handy one is groff, a derivative of the ancient troff andnroff families. (“roff” comes from “runoff”; man pages aretraditionally written in nroff format.)You can use gtbl with groff to quickly make nice PostScripttables.gtbl some.tr | groff > /tmp/some.psUnix Tools: Shells, part 4groff and gtbl.sp 10 # skip 10 lines.ps 14 # point size 14 pt.TS # table startcenter box tab(/); # center the table in the page, put it in a box, and make the delim a "/"c c c c # center the first liner r r r . # right justify the rest.sp .2v # skip down 2/10s of a lineLast / First / Age / Zipcode # column headers.sp .1v # skip down 1/10 of a line_ # horizontal rule_ # horizontal rule.sp .3v # skip down 3/10s of a lineGordon/Flash/91/91191 # record one.sp .2v # skip downJones/Carol/20/32399 # record two.sp .2v # skip downMiller/Bob/23/32499 # record three.sp .2v # skip downYagi/Akihito/22/32111 # record four.sp .1v # skip down.TE # table endUnix Tools: Shells, part 4fmtAnother great little utility is fmt which lets you quickly reformat adocument.You can use -w to control the width. fmt also prefers to see twospaces after a question mark, period, or exclamation point to indicatethe end of a sentence.Unix Tools: Shells, part 4fmt example$ cat lincoln.txtFour score and seven years ago our fathers brought forth on thiscontinent, a new nation, conceived in Liberty, and dedicated to theproposition that all men are created equal.Now we are engaged in a great civil war, testing whether that nation,or any nation so conceived and so dedicated, can long endure. We aremet on a great battle-field of that war. We have come to dedicate aportion of that field, as a final resting place for those who here gavetheir lives that that nation might live. It is altogether fitting and properthat we should do this.Unix Tools: Shells, part 4fmt example$ fmt lincoln.txtFour score and seven years ago our fathers brought forth on thiscontinent, a new nation, conceived in Liberty, and dedicated to theproposition that all men are created equal.Now we are engaged in a great civil war, testing whether that nation,or any nation so conceived and so dedicated, can long endure. We are meton a great battle-field of that war. We have come to dedicate a portionof that field, as a final resting place for those who here gave theirlives that that nation might live. It is altogether fitting and properthat we should do this.Unix Tools: Shells, part 4fmt example$ fmt -w 20 lincoln.txtFour score andseven years ago ourfathers broughtforth on thiscontinent, a newnation, conceivedin Liberty, anddedicated to theproposition thatall men are createdequal.Unix Tools: Shells, part 4cutcut allows you to extract columnar portions of a file. Thecolumns can be specified either by a delimiter (the defaultdelimiter is the tab character.)You can specify a delimiter with the -d option.You must specify either at least one field number with -f, a bytenumber with -b, or a character number with
View Full Document