Text File Filters
Text File Filters
The following commands work with text files only.
cat
Catenate means ``to connect in a series''. The cat command displays the contents of a text files. If more than one file is placed in the command line then the files are displayed in sudcession. It is here that cat derives its name. With the use of the redirection operator, multiple files can be placed within a single file. Examples:- cat myfile displays the text in `myfile'
- cat fileA fileB displays the text in fileA followed by the text in fileB.
- cat fileA fileB > fileC concatenates the contents of fileA and fileB and puts it into new file ``fileC''. (If fileC already existed, it is replaced.)
If a file is long (more than 20 lines) then cat is the wrong command for displaying. Instead, use more, which displays the contents of the file one screenful at a time:
- more myfile displays `myfile', one screenful at a time
head
To display only the first 10 lines of a text file, use head. Using a simple option, you can display more or less than 10 lines. Examples:- head myfile displays the top 10 lines of `myfile'
- head -7 myfile displays the top 7 lines of `myfile'
Similar to head, the command tail displays the final 10 lines of a text file: Examples:
- tail myfile displays the bottom 10 lines of `myfile'
- tail -7 myfile displays the bottom 7 lines of `myfile'
sort
The command sort rearranges the lines of text in ASCII order -- digits come before uppercase letters, which come before lowercase letters... There are lots of important options which refine the sorting criteria; read the manual page for sort. Examples:- sort myfile display the lines of `myfile' in sorted order
- sort -n myfile display the lines of `myfile' sorted numerically (so that, e.g., "9." comes before "11.")
- sort -f myfile sort with all lower-case letters ``folded'' into upper case (so that, e.g., "a" comes before "B")
- sort -k 3 myfile sort lines by their 3rd word
- sort -r myfile sort in reverse order
- sort -nrk 5 myfile sort the lines of `myfile' in reverse numerical order by the 5th field (word)
grep
Weird name -- great tool!! The command grep is used to cull the lines of a file which contain a given string. Read the manual page for grep for all the variations. Examples:- grep Name myfile list every line of the file `myfile' which contains the string "Name"
- grep -i name myfile list every line of the file `myfile' which contains any capilized variation of the string "name" (i.e., including NAME, nAmE, etc.)
- grep Home *.html list each line containing `Home' from any file in the current directory named with the .html suffix
- grep -v x myfile list every line which does NOT contain the letter `x'
- grep -c the myfile count the number of lines in `myfile' which contain the string "the"
wc
wc stands for ``word count'', and tells you the number of lines, the number of "words" (blocks of characters separated by whitespace), and the total number of characters in the file. Examples:- wc myfile line/word/character stats for the file `myfile'
- wc -l myfile number of lines in `myfile'
- wc -w myfile number of words in `myfile'
spell
The spell commands filters through a file looking for things that look like words, and spits out those which don't appear in the computers dictionary file. Examples:- spell myfile displays all the possible misspellings from the file `myfile'
- spell myfile > errors writes all the possible misspellings from the file `myfile' to a file named `errors'
