15.2.2 Changing Text Using the vi Editor

15.3 Viewing Files and String Settings

The cat command is used to view an ASCII file and there are various commands available. The cat command concatenates or displays files. The cat command reads each file parameter in sequence and writes it to standard output. If you do not specify a file name, the cat command reads from standard input. You can also specify a file name of - (dash) for standard input.

Note

Do not redirect output to one of the input files using the redirection symbol, > (caret). If you do this, you lose the original data in the input file because the shell truncates the file before the cat command can read it.

The cat command syntax is as follows:

cat [ -q ] [ -r ] [ -s ] [ -S ] [ -u ] [ -n [ -b ] ] [ -v [ -e ] [ -t ] ] [ - | File ... ]

Table 24 provides the flags that can be used with the cat command.


Table 24: The cat Command Flags and Flag Descriptions

Examples using the cat command are listed in the following:

15.3.1 Using the pg Command to View Files

Formats files to the display. The pg command reads a file name from the file parameter and writes the file to standard output one screen at a time. If you specify a - (dash) as the file parameter, or run the pg command without options, the pg command reads standard input. Each screen is followed by a prompt. If you press the Enter key, another page is displayed. Subcommands used with the pg command let you review or search in the file.

To determine workstation attributes, the pg command scans the file for the workstation type specified by the TERM environment variable. The default type is dumb.

The pg command syntax

pg [ -Number ] [ -c ] [ -e ] [ -f ] [ -n ] [ -p String ] [ -s ] [ +LineNumber ] [ +/Pattern/ ] [ File ... ]

Table 25 provides the flags that can be used with the pg command.


Table 25: The pg Command Flags and Flag Descriptions

When the pg command pauses and issues a prompt, you can issue a subcommand. Some of these subcommands change the display to a particular place in the file, some search for specific patterns in the text, and others change the environment in which the pg command works.

Table 26 provides a list of the subcommands for the pg command.


Table 26: pg Subcommands

Examples using the pg command are provided in the following:

15.3.2 Using the view Command to View Files

The view command starts the vi full-screen editor in read-only mode. The read-only mode is only advisory to prevent accidental changes to the file.

The view command syntax is as follows:

view [ -cSubcommand ] [ -l ] [ -t Tag ] [ -wNumber ] [ -y ] [ -r [ File ] ] [ + [ Subcommand ] ] [ File ... ]

Table 27 provides a list of the flags for the view command.


Table 27: Table of Flags for the view Command

Check 15.1 The vi File Editor to understand the vi editor.

15.3.3 Using the echo Command

The echo command writes character strings to standard output. Strings are separated by spaces, and a new-line character follows the last String parameter specified.

The echo command syntax is as follows:

echo [ String ... ]

The echo command recognizes the escape conventions as shown in Figure 36:


Table 28: Escape Conventions Used with the echo Command

Examples using the echo command are provided in the following:

15.3.4 Using the file Command to Determine a File Type

The file command reads the files specified by the File parameter or the FileList variable, performs a series of tests on each file, and attempts to classify them by type. The command then writes the file types to standard output.

If a file appears to be in ASCII format, the file command examines the first 1024 bytes and determines the file type. If a file does not appear to be in ASCII format, the file command further attempts to distinguish a binary data file from a text file that contains extended characters.

If the File parameter specifies an executable or object module file and the version number is greater than 0, the file command displays the version stamp.

To classify the file type, use the following command syntax:

file [-m MagicFile] [-f FileList] [File...]

To check the magic file for Format Errors, use the following command syntax:

file-c [-m MagicFile]

Table 29 provides the flags that can be used by the file command.


Table 29: The file Command Flags and Flag Descriptions

Examples using the file command are provided in the following:

15.3.5 Using the strings Command in Object or Binary Files

The strings command looks for printable strings in an object or binary file. A string is any sequence of 4 or more printable characters that end with a new-line or a null character. The strings command is useful for identifying random object files.

The strings command syntax is as follows:

strings [ -a ] [ - ] [ -o ] [ -t Format ] [ -n Number ] [ -Number ] [ File ... ]

Table 30 provides the flags that can be used with the strings command.


Table 30: The strings Command Flags and Flag Descriptions

Note

When the -o and the -t format flags are defined more than once on a command line, the last flag specified controls the behavior of the strings command.

Examples using the strings command are as follows:

In this section you learned how to edit a file using the vi editor. You also learned how to view a file with the pg and cat commands and the differences between the two. You can echo text to the screen or a file. The file characteristics can be found using the file command. You can also find strings within a file using the strings command. A lot of what you have learned in this section will be used in Chapter 16. The Shell, and Shell Scripts.

15.4 Quiz