Basic Linux tutorial-simple text processing
Introduction
This section describes the commandstr
(Note that it is not tar ),col
,join
,paste
. The actual section is a continuation of the previous section on commands that can perform pipeline operations, so we will still be familiar with the use of these commands in conjunction with pipelines.
I. common text processing commands II. Text processing commands 1.tr
The tr command can be used to delete some text in a piece of text information. Or convert it.
Usage:
tr [option]...SET1 [SET2]
Common options include:
Option |
Description |
-d |
Delete characters that match set1. Note that they are not full-term match or in character order. |
-s |
Removes the consecutive and repeated characters specified by set1 in the input text. |
Example:
# Delete All 'O', 'l', 'H' $ echo 'Hello shiyanlou 'in "hello shiyanlou" | tr-d 'olh' # Replace ll in "hello, de-duplicate into an l $ echo 'hello' | tr-s 'l' # convert all input text to uppercase or lowercase and output $ cat/etc/passwd | tr '[: lower:] ''[: upper:] '# Above' [: lower:]'' [: upper:] 'You can also write simply '[a-z]' [A-Z] ', of course, in turn, it is also possible to write in upper case.
For more use of tr, you can use--help
Orman tr
.
2. col command
The col command canTab
Replace it with a number of equal spaces or reverse the operation.
Usage:
col [option]
Common options include:
Option |
Description |
-x |
SetTab Convert to Space |
-h |
Convert spacesTab (Default) |
Example:
# View the invisible characters in/etc/protocols. You can see many ^ I, this is actually the symbol for converting A Tab to A visible character $ cat-A/etc/protocols # Use col-x to convert A Tab in/etc/protocols to A space, and then use cat to view it, you found ^ I missing $ cat/etc/protocols | col-x | cat-
3. join command
Users who have learned the database should be familiar with this. This command is used to merge the rows containing the same content in the two files.
Usage:
join [option]... file1 file2
Common options include:
Option |
Description |
-t |
Specifies the delimiter. The default Delimiter is space. |
-i |
Case Insensitive |
-1 |
Specifies the field used for comparison in the first file. By default, the first field is compared. |
-2 |
Specifies the field used for comparison in the second file. By default, the first field is compared. |
Example:
# Create two files $ echo '1 hello'> file1 $ echo '1 shiyanlou '> file2 $ join file1 file2 # merge/etc/passwd and/etc/shadow, specify ':' as the separator $ sudo join-t': '/etc/passwd/etc/shadow # merge the/etc/passwd and/etc/group files, use ':' as the separator to compare fields $ sudo join-t': '-1 4/etc/passwd-2 3/etc/group.
4. paste command
paste
This command correspondsjoin
The command is similar. It simply combines multiple files without comparing the dataTab
.
Usage:
paste [option] file...
Common options include:
Option |
Description |
-d |
Specifies the delimiter to merge. The default Delimiter is Tab. |
-s |
Not merged into one row. Each file is a row. |
Example:
$ echo hello > file1$ echo shiyanlou > file2$ echo www.shiyanlou.com > file3$ paste -d ':' file1 file2 file3$ paste -s file1 file2 file3
Iii. Summary
Not all of the above commands will be frequently used, but they are very practical and can reduce a lot of work, such as constantly copying and pasting values in gedit with the mouse, merge the contents of the two files into one file, which can be completed with only one command.
Job
1. Some special characters in Windows/dos and Linux/UNIX text files are inconsistent in the file packaging and decompression section. For example, the broken line character Windows is CR + LF (\r\n
), Linux/UNIX is LF (\n
). UseCat-A text
You can see the invisible special characters in the text. Linux\n
Is$
Windows/dos:^M$
, You can directly usedos2unix
Andunix2dos
The tool converts two formats.file
Command to view the specific types of files. However, if you do not use the preceding two conversion tools, you can use the commands you have learned to manually convert the dos text format to the UNIX text format.
2. Do you still remember the beebot game that I played on the Overlord when I was a child? Its orthodox name should be Space Invaders: Space Invaders.
Run the following command to install ninvaders:
sudo apt-get install ninvaders
This article permanently updates the link address: