click below
click below
Normal Size Small Size show me how
LPI101 - 5.2
Process Text Streams Using Filters
| Question | Answer |
|---|---|
| cut | Cut out (that is, print) selected columns or fields from one or more files. The source file is not changed. This is useful if you need quick access to a vertical slice of a file. By default, the slices are delimited by a tab character. |
| cut -blist | Print bytes in list positions. |
| cut -clist | Print characters in list columns. |
| cut -ddelim | Set field delimiter for -f. |
| cut -flist | Print list fields. |
| expand | Convert tabs to spaces. Sometimes the use of tab characters can make output that is attractive on one output device look bad on another. This command eliminates tabs and replaces them with the equivalent number of spaces. By default, tabs are assumed to b |
| expand -tnumber | Specify tab stops, in place of default 8. |
| expand -i | Initial; convert only at start of lines. |
| fmt | Format text to a specified width by filling lines and removing newline characters. Multiple files from the command line are concatenated. |
| fmt -u | Use uniform spacing: one space between words and two spaces between sentences. |
| fmt -w width | Set line width to width. The default is 75 characters. |
| head | Print the first few lines of one or more files (the "head" of the file or files). When more than one file is specified, a header is printed at the beginning of each file, and each is listed in succession. |
| head -c n | Print the first n bytes, or if n is followed by k or m, print the first n kilobytes or megabytes, respectively. |
| head -nn | Print the first n lines. The default is 10. |
| join | Print a line for each pair of input lines, one each from file1 and file2, that have identical join fields. This function could be thought of as a very simple database table join, where the two files share a common index just as two tables in a database wo |
| join -j1 field | Join on field of file1. |
| join -j2 field | Join on field of file2. |
| join -j field | Join on field of both file1 and file2. |
| nl | Number the lines of files, which are concatenated in the output. This command is used for numbering lines in the body of text, including special header and footer options normally excluded from the line numbering. The numbering is done for each logical pa |
| nl -b style | Set body numbering style to style, t by default. |
| nl -f style | Set footer number style to style, n by default. |
| nl -h style | Set header numbering style to style, n by default. |
| nl styles | A Number all lines. t Only number non-empty lines. n Do not number lines. pREGEXP Only number lines that contain a match for regular expression REGEXP. |
| od | Dump files in octal and other formats. This program prints a listing of a file's contents in a variety of formats. It is often used to examine the byte codes of binary files but can be used on any file or input stream. Each line of output consists of an o |
| od -t type | Specify the type of output. Typical types include: A Named character c ASCII character or backslash escape O Octal (the default) x Hexadecimal |
| paste | Paste together corresponding lines of one or more files into vertical columns. |
| paste -dn | Separate columns with character n in place of the default tab. |
| paste -s | Merge lines from one file into a single line. When multiple files are specified, their contents are placed on individual lines of output, one per file. |
| pr | Convert a text file into a paginated, columnar version, with headers and page fills. This command is convenient for yielding nice output, such as for a line printer from raw uninteresting text files. The header will consist of the date and time, the filen |
| pr -d | Double space. |
| pr -h header | Use header in place of the filename in the header. |
| pr -l lines | Set page length to lines. The default is 66. |
| pr -O width | Set the left margin to width. |
| sort | Write input to stdout, sorted alphabetically. |
| sort -f | Case-insensitive sort. |
| -kPOS1[,POS2] | Sort on the key starting at POS1 and (optionally) ending at POS2. |
| sort -n | Sort numerically. |
| sort -r | Sort in reverse order. |
| sort -tSEP | Use SEP as the key separator. The default is to use whitespace as the key separator. |
| split | Split infile into a specified number of line groups, with output going into a succession of files, outfileaa, outfileab, and so on (the default is xaa, xab, etc.). The infile remains unchanged. This command is handy if you have a very long text file that |
| split -n | Split the infile into n-line segments. The default is 1,000. |
| tac | This command is named as an opposite for the cat command, which simply prints text files to standard output. In this case, tac prints the text files to standard output with lines in reverse order. |
| tail | Print the last few lines of one or more files (the "tail" of the file or files). When more than one file is specified, a header is printed at the beginning of each file, and each is listed in succession. |
| tail -Cn | This option prints the last n bytes, or if n is followed by k or m, the last n kilobytes or megabytes, respectively. |
| tail -nm | Prints the last m lines. The default is 10. |
| tail -f | Continuously display a file as it is actively written by another process. This is useful for watching log files as the system runs. |
| tr | Translate characters from string1 to the corresponding characters in string2. tr does not have file arguments and therefore must use standard input and output. |
| unexpand | Convert spaces to tabs. This command performs the opposite action of expand. By default, tab stops are assumed to be every eight spaces. |
| unexpand -a | Convert all spaces, not just leading spaces. Normally unexpand will only work on spaces at the beginning of each line of input. Using the -a option causes it to replace spaces anywhere in the input. |
| unexpand -t number | Specify tab stops, in place of default 8. |
| uniq | Writes input (or stdin) to output (or stdout), eliminating adjacent duplicate lines. Since uniq works only on adjacent lines of its input, it is most often used in conjunction with sort. |
| uniq -d | Print only non-unique (repeating) lines. |
| uniq -u | Print only unique (non-repeating) lines. |
| wc | Print counts of characters, words, and lines for files. When multiple files are listed, statistics for each file output on a separate line with a cumulative total output last. |
| wc -c | Print the character count only. |
| wc -l | Print the line count only. |
| wc -w | Print the word count only. |
| xargs | Execute command followed by its optional initial-arguments and append additional arguments found on standard input. Typically, the additional arguments are filenames in quantities too large for a single command line. xargs runs command multiple times to e |
| xargs -n maxargs | Limit the number of additional arguments to maxargs for each invocation of command. |
| xargs -p | Interactive mode. Prompt the user for each execution of command. |