Vous êtes sur la page 1sur 2

Text File Filters

11-09-27 11:50 PM

Text File Filters


The following commands work with text files only.
cat

cat more head tail sort grep wc spell

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: 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.)
cat myfile

more

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

Press the spacebar to advance by one screenful. Press the Return key to advance by only one line. Press the Q key to quit.
head

To display only the first 10 lines of a text file, use display more or less than 10 lines. Examples:
head myfile

head.

Using a simple option, you can

displays the top 10 lines of `myfile' head -7 myfile displays the top 7 lines of `myfile'
tail

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


Page 1 of 2

http://amath.colorado.edu/computing/unix/text.html#wc

Text File Filters

11-09-27 11:50 PM

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 sort -n myfile display the lines

before "11.")
sort -f myfile

`myfile' in sorted order of `myfile' sorted numerically (so that, e.g., "9." comes

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

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 wc myfile wc -l myfile wc -w myfile

line/word/character stats for the file `myfile' number of lines in `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'

named `errors'

to a file

http://amath.colorado.edu/computing/unix/text.html#wc

Page 2 of 2

Vous aimerez peut-être aussi