Tips for using cat commands

Source: Internet
Author: User
Tags printable characters

In our continuous series of GNU text utilities, Jacek artymiak has done a bit of research on cat-the cat command is a favorite of UNIX lovers and hated by UNIX lovers.

You often need to process several files into one and save the processing results to a separate output file.cat(Concatenate) The command accepts one or more files on its input and prints them as a separate file to its output. For example,cat chapter01 chapter02 chapter03 > bookSetchapterXXThe file is stored in a separatebookFile.

Input filescatThe order after the command is printed. Therefore, to change the order of information, you must first change the order of input files. In addition, when the number of files to be processed is too large for you to manually enter the names of these files, you can use wildcards, suchcat chapter* > bookRemember, the file names will be listed in ascending order. When you suddenly findchapter13When it is sent to the outputchapter2 BeforeInchapter02 AfterThis will cause very interesting problems.

WhencatWhen the output is not redirected to the standard output of one file or another command,catSimilar to most command line tools, the output is sent to the console. This means you can usecatTo display files. For example, you can usecat /etc/passwdTo display the contents of the system password file. For convenience, you should uselessView large files, as shown inless /etc/passwd(You can enterman lessLearn more aboutlessKnowledge ).

AlthoughcatIt is mainly used to merge files. You can also use it for simple automatic processing of input. For example, you can use a separate blank line to remove multiple blank lines (Use-sOption). This is a good way to clear the source code before it is publicly available. Unfortunately,catThere is no option for clearing all blank rows at a time. But this is not a big problem, because you can use it easily.sedCommand to remove these blank lines:

Listing 1. Use SED and cat to remove blank lines

$ cat -s /etc/X11/XF86Config | sed '/^[[:space:]]*$/d'...# Multiple FontPath entries are allowed (they are concatenated together)# By default, Red Hat 6.0 and later now use a font server independent of# the X server to render fonts.    FontPath   "/usr/X11R6/lib/X11/fonts/TrueType"    FontPath   "unix/:7100"EndSection...

Source files that read configuration files and HTML pages, especially those that are generated by scripts that insert unnecessary new lines, and those that contain large condition structures (each item has been separated by blank lines) in the source file, empty line tightening is a convenient technique.

catAnother important feature is that it can number rows. This function is convenient for the compilation of procedural documents and legal and scientific documents. The row number printed on the left makes it easy to reference a part of the document. This is important in programming, scientific research, business reports, and even legislation. The row numbering function has two options:-bOption (you can only number non-blank rows) and-nOption (all rows can be numbered ):

Listing 2. Numbering rows

$ cat -b /etc/X11/XF86Config...    14  # Multiple FontPath entries are allowed (they are concatenated together)    15  # By default, Red Hat 6.0 and later now use a font server independent of    16  # the X server to render fonts.    17      FontPath   "/usr/X11R6/lib/X11/fonts/TrueType"    18      FontPath   "unix/:7100"    19  EndSection...$ cat -n /etc/X11/XF86Config...    20  # Multiple FontPath entries are allowed (they are concatenated together)    21  # By default, Red Hat 6.0 and later now use a font server independent of    22  # the X server to render fonts.    23      24      FontPath   "/usr/X11R6/lib/X11/fonts/TrueType"    25      FontPath   "unix/:7100"    26      27  EndSection...

catYou can also help when viewing files containing non-printable characters such as tabs. You can use the following options to display tabs:

  • -TDisplay the tab^I
  • -vNon-printable characters are displayed. Besides line breaks and tabs, they use a "control sequence" with the same effect ". For example, when you process a file generated in windows, this file uses control-M (^M) To mark the end of the row. For characters with code greater than 127M-("Meta"), which is added before the character in other systemsAlt-Equivalent.
  • -EAdd the dollar sign ($).

 

Listing 3. displaying non-printable characters

$ cat -t /etc/X11/XF86Config...# Multiple FontPath entries are allowed (they are concatenated together)# By default, Red Hat 6.0 and later now use a font server independent of# the X server to render fonts.^IFontPath^I"/usr/X11R6/lib/X11/fonts/TrueType"^IFontPath^I"unix/:7100"EndSection...$ cat -E /etc/X11/XF86Config...# Multiple FontPath entries are allowed (they are concatenated together)$# By default, Red Hat 6.0 and later now use a font server independent of$# the X server to render fonts.$$    FontPath   "/usr/X11R6/lib/X11/fonts/TrueType"$    FontPath   "unix/:7100"$$EndSection$...$ cat -v /etc/X11/XF86Config...^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@M-|M-8^X^@^@^@P^@^O"M-X^O M-@^M^@^@^@M-^@^O"M-@M-k^@M-8*^@@M-^H$M-@M-9|A(M-@)M-yM-|M-sM-*M-hW^A^@^@j^@M-|M-sM-%1M-@M-9^@^B^@^@M-sM-+fM-^A= ^@ ^@F^@^@   ^@M-9^@^H^@^@M-sM-$M-G^E(l!M-@M-^?^IM-A5^@^@^D^@PM-^]M-^/X1M-H%^@^@^D^@tyM-G...

Next, let's take a look.headAndtail. See you later! At the same time, if you have any questions or comments, I will be happy to hear from you-you can email to the jacek@artymiak.com.

References

  • For more information, see the original article on the developerworks global site.

  • In this series, the introduction of the text utility to the opponent's album page and information page is a supplement. To learn more about this, open a new terminal window and enterman catOrinfo cat-Alternatively, you can open a new browser window and view the cat manual page on gnu.org.
  • You can also refer to getting started with Cat chat files. Understand whycatIt is considered to be both an excellent user interface design example and a typical example of poor user interface design.
  • You can find a list of more useful GNU tools in the GNU text utilities Manual (expanded view in the same directory of MIT) find out more about the tools we know and like (they are all text utilities.
  • Windows users can find these tools in the cygwin software package.
  • Mac OS x users may want to try Fink. It installs a variety of UNIX environments in Mac OS X.
  • Have you encountered some problems? For more information, see the GNU text utility FAQ.
  • Do I need more introductory information before studying the tools described here? You can start from unixhelp for users.
  • Or try the introductory article "Skills: Understanding textutils" in this series "(Developerworks, January 1, October 2002 ).
  • Classic Works in this field are UNIX power tools, Jerry peek, Tim O 'Reilly, and Mike loukides (O 'Reilly and Associates, 1997 ); ISBN 1-56592-260-3.
  • InDeveloperworksIn the Linux area, find the Linux references you are looking.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.