Introduction to Simple Text processing experiments
In this section we will introduce these commands tr
(note not tar),, col
, join
paste
. The actual section is a continuation of the commands that enable pipeline operations, so we will still be familiar with the use of these commands in conjunction with pipelines.
First, the common Text Processing command two, the Text Processing command 1.tr command
The TR command can be used to delete some text from a piece of textual information. or convert it.
How to use:
tr [option]...SET1 [SET2]
The common options are:
Options |
Description |
-d |
Delete and Set1 matching characters, note that not all words match or match by character order |
-s |
Removes set1 specified characters that are contiguous and repeated in the input text |
Examples of operations:
# 删除 "hello shiyanlou" 中所有的‘o‘,‘l‘,‘h‘$ echo ‘hello shiyanlou‘ | tr -d ‘olh‘# 将"hello" 中的ll,去重为一个l$ echo ‘hello‘ | tr -s ‘l‘# 将输入文本,全部转换为大写或小写输出$ cat /etc/passwd | tr ‘[:lower:]‘ ‘[:upper:]‘# 上面的‘[:lower:]‘ ‘[:upper:]‘你也可以简单的写作‘[a-z]‘ ‘[A-Z]‘,当然反过来将大写变小写也是可以的
More TR use, you can use --help
or man tr
get.
2.col command
The col command can be Tab
built with a number of equivalent spaces, or reverse the operation.
How to use:
col [option]
The common options are:
Options |
Description |
-x |
will be Tab converted to spaces |
-h |
Convert a space to Tab (the default option) |
Examples of operations:
# 查看 /etc/protocols 中的不可见字符,可以看到很多 ^I ,这其实就是 Tab 转义成可见字符的符号$ cat -A /etc/protocols# 使用 col -x 将 /etc/protocols 中的 Tab 转换为空格,然后再使用 cat 查看,你发现 ^I 不见了$ cat /etc/protocols | col -x | cat -A
3.join command
Users who have studied the database should not be unfamiliar with this, and this command is used to merge the same line that contains the same content in two files.
How to use:
join [option]... file1 file2
The common options are:
Options |
Description |
-t |
Specify delimiter, default is space |
-i |
Ignore Case differences |
-1 |
Indicates which field the first file is to compare with, and the default compares the first one |
-2 |
Indicates which field to use for the second file, and by default compares the first one |
Examples of operations:
# 创建两个文件$ echo ‘1 hello‘ > file1$ echo ‘1 shiyanlou‘ > file2$ join file1 file2# 将/etc/passwd与/etc/shadow两个文件合并,指定以‘:‘作为分隔符$ sudo join -t‘:‘ /etc/passwd /etc/shadow# 将/etc/passwd与/etc/group两个文件合并,指定以‘:‘作为分隔符, 分别比对第4和第3个字段$ sudo join -t‘:‘ -1 4 /etc/passwd -2 3 /etc/group
4.paste command
paste
This command join
is similar to a command, which simply merges multiple files together to separate them without comparing the data Tab
.
How to use:
paste [option] file...
The common options are:
Options |
Description |
-d |
Specifies the merged delimiter, which is tab by default |
-s |
Do not merge to one line, one row for each file |
Examples of operations:
echo hello > file1$ echo shiyanlou > file2$ echo www.shiyanlou.com > file3$ paste -d ‘:‘ file1 file2 file3$ paste -s file1 file2 file3
Third, summary
The above commands are not all you will often use, but they are very practical, skilled, can alleviate a lot of work, such as the use of mouse operations in the gedit copy paste assignment paste, the contents of two files into a file, which originally only need a command to complete.
Homework
1, in the "File Packaging and Decompression" section of the experiment mentioned Windows/dos and Linux/unix text file some special characters inconsistent, such as the break character Windows is Cr+lf ( \r\n
), Linux/unix is LF ( \n
). Use cat -A 文本
to see the invisible special characters contained in the text. The performance of Linux \n
is one $
, and Windows/dos's performance is ^M$
that you can directly use dos2unix
and unix2dos
tools to convert between the two formats, using file
commands to view the specific type of file. But now I hope that you do not use the above two conversion tools, using the previously learned command to manually complete the DOS text format to the UNIX text format conversion.
2. Do you remember the little bee games that I played on the little bully when I was a kid? Its orthodox name should be space Invaders: the Space Invaders.
The following command can be used to install, the reason is called ninvaders because the game is based on the ncurses command line graphics library do:
sudo apt-get install ninvaders
1.9 Simple Text processing (learning process)