Available CategoriesAdobeMacromediaProgrammingSQLServer AdministrationNetworkingMicrosoft ProductsMac OSLinux systemsMobile devicesXMLCertificationMiscAvailable TutorialsLearning red hat enterprise linux and fedoraRed Hat Linux 9 Professional Secretsred hat linux bible. fedora and enterprise editionEmbedded Linux SystemsSecure Linux-based ServersRed HatLinux securityRunning LinuxMoving to LinuxCluster Computing with LinuxHow linux worksPDF hacksPpractical unix & internet security |
Text UtilitiesText UtilitiesThe GNU text utility package includes a large number of utilities to manipulate the contents of text files. These utilities are patterned after the UNIX commands of the same name. The GNU versions of the programs usually have additional options and are optimized for speed. In general, the GNU utilities do not have any of the arbitrary limitations of their UNIX counterparts. Table 8-4 briefly describes each text utility. The best way to learn these utilities is to try each one out. Before trying out a program, type info progname or man progname (where progname is the name of the program) to view the online help information.
Counting Words and Lines in a Text FileFor example, suppose that you want to use the wc command to display the character, word, and line count of a text file. Try the following:
wc /etc/inittab
54 236 1698 /etc/inittab
This causes wc to display the number of lines (54), words (236), and characters (1698) in the /etc/inittab file. If you simply want to see the number of lines in a file, use the -l option:
wc -l /etc/inittab
54 /etc/inittab
As you can see, in this case, wc simply displays the line count. If you don’t specify a filename, the wc command expects input from the standard input. You can use the pipe feature of the shell to feed the output of another command to wc. This can be handy sometimes. Suppose that you want a rough count of the processes running on your system. You can get a list of all processes with the ps ax command, but instead of manually counting the lines, just pipe the output of ps to wc, and you can get a rough count, as follows:
ps ax | wc -l
65
That means that the ps command has produced 65 lines of output. Because the first line simply shows the headings for the tabular columns, you can estimate that about 64 processes are running on your system. (Of course, this count probably includes the processes used to run the ps and wc commands as well, but who’s counting?) Sorting Text FilesYou can sort the lines in a text file by using the sort command. To see how the sort command works, first type more /etc/passwd to see the current contents of the /etc/passwd file. Now, type sort /etc/passwd to see the lines sorted alphabetically. If you want to sort a file and save the sorted version in another file, you have to use the Bash shell’s output redirection feature, as follows: sort /etc/passwd > ~/sorted.text This command sorts the lines in the /etc/passwd file and saves the output in a file named sorted.text in your home directory. Substituting or Deleting Characters from a FileAnother interesting command is tr—it substitutes one group of characters for another (or deletes a selected character) throughout a file. The tr command is useful when you want to convert a text file from one operating system to another because different operating systems use different special characters to mark the end of a line of text. Splitting a File into Several Smaller FilesThe split command is handy when you want to copy a file to a floppy disk but the file is too large to fit on a single floppy. You can then use the split command to break up the file into smaller files, each of which can fit on a floppy. By default, split puts 1,000 lines into each file. The files are named by groups of letters such as aa, ab, ac, and so on. You can specify a prefix for the filenames. For example, to split a large file called hugefile.tar into smaller files that fit onto several high-density 3.5-inch floppy disks, use split as follows: split -b 1440k hugefile.tar part. This command splits the hugefile.tar file into 1,440K chunks so that each can fit onto a floppy disk. The command creates files named part.aa, part.ab, part.ac, and so on. To combine the split files back into a single file, use the cat command as follows: cat part.?? > hugefile.tar
|