Introduction
In this section, we will introduce tr
, col
, join
, and paste
and will still review the pipeline to familiarize ourselves with these commands.
In this section, we will introduce tr
, col
, join
, and paste
and will still review the pipeline to familiarize ourselves with these commands.
The command tr
can be used to delete word(s) in a piece of text or to convert it.
tr [option]...SET1 [SET2]
tr
Option | Descripion |
---|---|
-d |
Delete characters in string1 from the input. |
-s |
Remove the characters specified by set1 that are consecutive and repeated in the input text. |
Delete all of the 'o', 'l' and 'h' characters in "hello labex":
echo 'hello labex' | tr -d 'olh'
Delete duplicated char 'l' in 'hello':
echo 'hello' | tr -s 'l'
Show the input text in uppercase or lowercase. For example, the following commands show in uppercase:
cat /etc/passwd | tr '[:lower:]' '[:upper:]'
## or cat /etc/passwd | tr '[a-z]' '[A-Z]'
labex:project/ $ echo 'hello labex' | tr -d 'olh'
e abex
labex:project/ $ echo 'hello' | tr -s 'l'
helo
labex:project/ $ cat /etc/passwd | tr '[:lower:]' '[:upper:]'
...
For more uses of tr
, use tr --help
or man tr
to fetch further information.
col
can replace the Tab
character with several space
characters or reverse the operation.
col [option]
col
Option | Description |
---|---|
-x |
Convert Tab to multiple spaces. |
-h |
Convert Spaces to Tab (Default). |
To view invisible characters in /etc/protocols
: We can see a lot of ^I
in the file. This is Tab
transformed into visible characters.
cat -A /etc/protocols
Use col -x
to convert tabs in /etc/protocols
to spaces, then use cat to see the file content, and we will find that ^I
is gone.
cat /etc/protocols | col -x | cat -A
labex:project/ $ cat /etc/protocols | col -x | cat -A
## Internet (IP) protocols$
#$
## Updated from http://www.iana.org/assignments/protocol-numbers and other$
## sources.$
## New protocols will be added on request if they have been officially$
## assigned by IANA and are not historical.$
...
The join
utility performs an "equality join'' on the specified files and writes the result to the standard output. The "join field'' is the field in each file by which the files are compared.
join [option]... file1 file2
join
Option | Description |
---|---|
-t |
Specify the delimiter. The default is a space. |
-i |
Ignore case differences. |
-1 field |
Indicate which field to use for the first file to compare. Default is the first field. |
-2 field |
Indicate which field to use for the second file to compare. Default is the first field. |
## Create two files
echo '1 hello' > file1
echo '1 labex' > file2
join file1 file2
labex:project/ $ echo '1 hello' > file1
echo '1 labex' > file2
join file1 file2
1 hello labex
paste
is similar to join
. It simply merges multiple files without comparison.
paste [option] file...
Option | Description |
---|---|
-d |
Specify the merged delimiter, which defaults to the tab. |
-s |
Concatenate all of the input files' lines in command-line order. |
echo hello > file1
echo labex > file2
echo labex.io > file3
paste -d ':' file1 file2 file3
echo
paste -s file1 file2 file3
hello:labex:labex.io
hello
labex
labex.io
Please use the following command to install Space Invaders:
sudo apt-get install ninvaders -y
ninvaders
In the "File Packing and Compression" section, we mentioned that some special characters in text files on Windows/DOS
and Linux/UNIX
are not the same, such as the line break: It is CR+LF (\r\n
) on Windows and LF (\n
) on Linux/UNIX
. We can use the cat -A text
to see the invisible special characters contained in the text.