Home >System Tutorial >LINUX >Detailed explanation of two extremely useful and important text manipulation commands on Linux

Detailed explanation of two extremely useful and important text manipulation commands on Linux

WBOY
WBOYforward
2024-02-14 17:27:03648browse

Linux system makes text manipulation and processing very easy through a large number of commands. Command line tools like tr and fmt can help you change, format, and modify text files from the terminal.

详解 Linux 上两个超有用重要的文本操作命令

In this article, we will discuss how to format text using tr and fmt commands in different situations. So, what are tr and fmt? And how do you use them?

What is the tr command?

tr command is the abbreviation of the word translate, which can help users delete or replace specific characters in text files. It is typically used in conjunction with other commands and works by taking standard input, performing operations on it, and writing the results to standard output.

tr command syntax

The basic syntax of the tr command is:

 tr [option]... SET1 [SET2]

Range of character set:

  • \NNN Characters with octal value NNN (1 to 3 are characters with octal value)
  • \ backslash
  • \a Ctrl-G Ringtone
  • \b Ctrl-H backspace character
  • \f Ctrl-L Walk and page change
  • \n Ctrl-J New line
  • \r Ctrl-M Enter
  • \t Ctrl-I tab
  • \v Ctrl-X horizontal tab
  • CHAR1-CHAR2: Specify the character range from CHAR1 to CHAR2. The specification of the range is based on the order of ASCII codes, which can only be from small to large, not from large to small.
  • [CHAR*]: This is a special setting for SET2. Its function is to repeat the specified characters until they are the same length as SET1
  • [CHAR*REPEAT]: This is also a special setting for SET2. The function is to repeat the specified characters until the set REPEAT times (the number of REPEAT is calculated in octal system, starting with 0)
  • [:alnum:] : All alphabetic characters and numbers
  • [:alpha:] : All alphabetic characters
  • [:blank:] : All horizontal spaces
  • [:cntrl:] : All control characters
  • [:digit:]: All digits
  • [:graph:]: All printable characters (excluding spaces)
  • [:lower:] : All lowercase letters
  • [:print:]: All printable characters (including spaces)
  • [:punct:] : All punctuation characters
  • [:space:]: All horizontal and vertical space characters
  • [:upper:] : all uppercase letters
  • [:xdigit:]: All hexadecimal numbers
  • [=CHAR=]: All characters that match the specified characters (CHAR in the equal sign represents the characters you can customize)

How to use the tr command

You can use the tr command to change the case of a text file, remove numbers, replace spaces with tabs, and even put each word on a new line. The possibilities with the tr command are so vast and you can achieve so much if you know how to use it.

For this example, create a text file: linuxmi.txt. The content of the file is:

Linuxmi.com. Technology, Simplified! 56789

Use tr command to replace characters

To do this, specify the characters to replace and what to replace them with. For example, if you want to replace the first character of each word with ABCD, execute this command:

The cat command is used to display the contents of the file.

cat linuxmi.txt | tr MTS5 ABCD

The cat command is used to display the contents of the file.

result:

Linuxmi.com. Bechnology, Cimplified! D6789
详解 Linux 上两个超有用重要的文本操作命令

Change text from lowercase to uppercase

To do this, execute the following command:

cat linuxmi.txt | tr a-z A-Z
cat linuxmi.txt | tr [:lower:] [:upper:]

linuxmi@linuxmi /home/linuxmi/www.linuxmi.com                                     
⚡ cat linuxmi.txt | tr a-z A-Z

The result is

Linuxmi.com. Technology, Simplified! 56789
详解 Linux 上两个超有用重要的文本操作命令

Change text from uppercase to lowercase

To do this, execute any of the following commands:

cat linuxmi.txt | tr A-Z a-z
cat linuxmi.txt | tr [:upper:] [:lower:]

linuxmi@linuxmi /home/linuxmi/www.linuxmi.com                                     
⚡ cat linuxmi.txt | tr a-z A-Z

The result is

Linuxmi.com. Technology, Simplified! 56789

Replace spaces with tabs

To replace all spaces with tabs, execute the following command:

linuxmi@linuxmi:~/www.linuxmi.com$ cat linuxmi.txt | tr [:space:] '\t'
详解 Linux 上两个超有用重要的文本操作命令

Alternatively, if you want to replace spaces with newlines to print each word on a separate line, replace "\t" with "\n".

Use tr command to delete characters

To remove characters, you can use the -d option next to the tr command. For example, if you want to remove the letter "e" from every word in the linuxmi.txt file, execute the following command:

linuxmi@linuxmi /home/linuxmi/www.linuxmi.com                                     
⚡ cat linuxmi.txt | tr -d 'e'

The result is

Linuxmi.com. Tchnology, Simplifid! 56789
详解 Linux 上两个超有用重要的文本操作命令

删除所有标点符号

如果你想删除文件中的所有标点符号而不指定是什么符号,使用 -d 选项和解释的序列 [:punct:]:

linuxmi@linuxmi:~/www.linuxmi.com$ cat linuxmi.txt | tr -d [:punct:]
结果为
Linuxmicom Technology Simplified 56789
详解 Linux 上两个超有用重要的文本操作命令

使用tr删除所有数字

删除文本文件中的所有数字,使用这个命令:

linuxmi@linuxmi:~/www.linuxmi.com$ cat linuxmi.txt | tr -d [:digit:]
结果为
Linuxmi.com. Technology, Simplified!
详解 Linux 上两个超有用重要的文本操作命令

使用 tr 补码选项

tr 命令附带 -c 选项,基本上执行与原始操作相反的操作。让我们以前面的命令为例。如果您将补码选项添加到此命令,它将删除所有非数字字符。看看:

linuxmi@linuxmi:~/www.linuxmi.com$ cat linuxmi.txt | tr -cd [:digit:]
结果为
56789
详解 Linux 上两个超有用重要的文本操作命令

什么是 fmt 命令?

fmt 命令是一个简单的文本格式化工具,您可以使用它在 Linux 上打印和处理文本。您还可以使用该命令格式化电子邮件回复。

要获得关于 fmt 命令的命令行帮助,运行以下命令查看其手册页面:

man fmt
详解 Linux 上两个超有用重要的文本操作命令

fmt命令格式

fmt命令的基本语法是:

fmt [-WIDTH] [OPTION] [FILE]

如何使用 fmt 命令

对于本例,创建一个名为file.txt的文件,并添加以下文本:

Linux fan www.linuxmi.com shares open source news, tutorials on Linux, programming, big data, operations, and databases. I was a 

big brother back then, the webmaster of Linux fanatics, and a Linux enthusiast using the desktop version. I write in my spare 

time and hope to share some useful tips with Linux beginners and enthusiasts.


使用默认 fmt 命令

默认的 fmt 命令优化了文本,并以更可读的格式打印出来。fmt 命令的默认宽度是 75 列。执行这个命令来尝试默认的 fmt 操作:

linuxmi@linuxmi:~/www.linuxmi.com$ fmt linuxmi.txt

结果为

详解 Linux 上两个超有用重要的文本操作命令

更改文本的宽度

要更改文件的宽度,可以使用-w选项。语法如下所示:

fmt -w N filename

修改文件宽度为20列,使用命令:

linuxmi@linuxmi:~/www.linuxmi.com$ fmt -w 50 linuxmi.txt

结果如下图

详解 Linux 上两个超有用重要的文本操作命令

拆分长行文本

要分割长行,请使用 -s 选项试一下:

linuxmi@linuxmi:~/www.linuxmi.com$ fmt -s linuxmi.txt

结果如下图为

详解 Linux 上两个超有用重要的文本操作命令

缩进每个段落的第一行

要通过缩进突出显示每个段落的第一行,请使用-t选项。这是语法:

fmt -t filename.

让我们试一试:

linuxmi@linuxmi:~/www.linuxmi.com$ fmt -t linuxmi.txt

结果为

详解 Linux 上两个超有用重要的文本操作命令

tr和fmt:有用的文本操作命令

tr 和 fmt 使用它们提供的不同操作使文本格式化和处理变得简单和自动化。Linux 和其他基于 Unix 的操作系统为您的每一个文本格式化需求提供了大量的文本操作命令。你只需要知道它们是什么以及如何使用它们。


The above is the detailed content of Detailed explanation of two extremely useful and important text manipulation commands on Linux. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:lxlinux.net. If there is any infringement, please contact admin@php.cn delete