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 > book
SetchapterXX
The file is stored in a separatebook
File.
Input filescat
The 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* > book
Remember, the file names will be listed in ascending order. When you suddenly findchapter13
When it is sent to the outputchapter2
BeforeInchapter02
AfterThis will cause very interesting problems.
Whencat
When the output is not redirected to the standard output of one file or another command,cat
Similar to most command line tools, the output is sent to the console. This means you can usecat
To display files. For example, you can usecat /etc/passwd
To display the contents of the system password file. For convenience, you should useless
View large files, as shown inless /etc/passwd
(You can enterman less
Learn more aboutless
Knowledge ).
Althoughcat
It 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-s
Option). This is a good way to clear the source code before it is publicly available. Unfortunately,cat
There is no option for clearing all blank rows at a time. But this is not a big problem, because you can use it easily.sed
Command 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.
cat
Another 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:-b
Option (you can only number non-blank rows) and-n
Option (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... |
cat
You can also help when viewing files containing non-printable characters such as tabs. You can use the following options to display tabs:
-T
Display the tab^I
-v
Non-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.
-E
Add 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.head
Andtail
. 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 enter
man cat
Orinfo 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 why
cat
It 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.