Make your own free website on Tripod.com
Search:
Keywords:
In Association with Amazon.com

More Basic Commands for Reading Text Files

You've already been introduced to several basic shell prompt commands for reading files in text editors. Here are just a few more.

The head Command

You can use the head command if you just want to look at the beginning of a file. The command is:

head <filename>

Head can be a useful command, but because it's limited to the first several lines, you won't see how long the file actually is. By default, you can only read the first ten lines of a file, although you can change this number by specifying a number, as in the following command:

head -20 <filename>

The tail Command

The reverse of head is tail. Using tail, you can review the last ten lines of a file.

The grep Command

The grep command is useful for finding specific character strings in a file. For example, if you want to find every reference made to "coffee" in the file sneakers.txt, you would type:

grep coffee sneakers.txt

You would see every line in that file where the word "coffee" is found.

Tip

Remember Case

 

Unless otherwise specified, grep searches are case sensitive. That means that searching for Coffee is different than searching for coffee. So among grep's options is -i, which allows you to make a case-insensitive search through a file. Read the grep man page for more about this command.

I/O Redirection and Pipes

You can use pipes and output redirection when you want to store and/or print information to read at a later time.

You can, for example, use grep to search for particular contents of a file, then have those results either saved as a file or sent to a printer.

To print the information about references to "coffee" in sneakers.txt, for example, just type:

grep coffee sneakers.txt | lpr

Wildcards and Regular Expressions

What if you forget the name of the file you're looking for? Using wildcards or regular expressions, you can perform actions on a file or files without knowing the complete filename. Just fill out what you know, then substitute the remainder with a wildcard.

Tip

More Information About Wildcards and Regular Expressions

 

To read more about wildcards and regular expressions, take a look at the bash man page (man bash). Remember that you can save the file to a text file by typing man bash | col -b > bash.txt. Then, you can open and read the file with less or pico (pico bash.txt). If you want to print the file, be aware that it is quite long.

We know the file's called "sneak____.txt," so just type:

ls sneak*.txt

and there's the name of the file:

sneakers.txt

You'll probably use the asterisk (*) most frequently when you're searching. The asterisk will search out everything that matches the pattern you're looking for. So even by typing:

ls *.txt

or:

ls sn*

You'd find sneakers.txt and any other files whose names begin with "sn." It helps to narrow your search as much as possible.

One way to narrow a search is to use the question mark symbol (?). Like the asterisk, using ? can help locate a file matching a search pattern.

In this case, though, ? is useful for matching a single character, so if you were searching for sneaker?.txt, you'd get sneakers.txt as a result, and/or sneakerz.txt, if there were such a filename.

When an asterisk, for example, just happens to be part of a filename, as might be the case if the file sneakers.txt was called sneak*.txt, that's when regular expressions can be useful.

Regular expressions are more complex than the straightforward asterisk or question mark.

Using the backslash (\), you can specify that you don't want to search out everything by using the asterisk, but you're instead looking for a file with an asterisk in the name.

If the file is called sneak*.txt, then, type:

sneak\*.txt

Here is a brief list of wildcards and regular expressions:

Book Site Map ElectronicVideo

Amazon.com International Sites :

USA, United Kingdom, Germany, France