linux tr 命令_在Linux中使用tr命令玩角色

linux tr 命令

tr command in Linux translates one set of characters to another. It can replace a character or a set of characters by another character or set of characters. tr reads the input from standard input and displays the output on standard output. Input can also be given in a file or by using echo command.

Linux中的tr命令将一组字符转换为另一组字符。 它可以用另一个字符或一组字符替换一个字符或一组字符。 tr从标准输入中读取输入,并在标准输出上显示输出。 输入也可以在文件中或使用echo命令给出。

tr is short for translate.

tr翻译的缩写。

The standard format for tr command is :

tr命令的标准格式为:


$ tr [option] [char_set 1] [char_set 2]

Based on the option(s) specified the tr command replaces the set of characters in “set 1” by “set 2”.

根据指定的选项,tr命令将“ set 1”中的字符集替换为“ set 2”。

替换字符 (Replacing characters)

To replace characters using tr command simply mention the characters to be replaced in 1st set and characters that are to be put in their place after replacing in 2nd set.

要使用tr命令替换字符,只需提及要在第一组中替换的字符和要在第二组中替换后放置的字符。


$ tr 'a' '1'

This command will wait for the input from STDIN. After getting the input, output on the screen will appear with all instances of ‘a’ replaced by ‘1’.

该命令将等待来自STDIN的输入。 获得输入后,屏幕上将出现输出,所有'a'实例均由'1'代替。

Simple Tr command

1.在tr命令中使用echo (1. Using echo with tr command)

The example above reads input from STDIN. Echo command can provide input along with tr command. Use Pipe(|) operator to run the commands together.

上面的示例从STDIN读取输入。 Echo命令可以与tr命令一起提供输入。 使用Pipe(|)运算符一起运行命令。


$  echo "apples and bananas" | tr 'a' '1'
Echo Tr

2.从文件中获取输入 (2. Taking input from a file)

tr can also take its input from a file. This is useful when the translation is to be done over a voluminous collection of text. Redirection (<) operator is used to give input from a file.

tr还可以从文件中获取其输入。 当要在大量文本上进行翻译时,这很有用。 重定向(<)运算符用于提供文件输入。


$ tr 'a' '1' < input.txt
Tr And File

input.txt contains the same text as the example above.

input.txt包含与上面的示例相同的文本。

To save the text to a file use redirection(>) operator to redirect the output to a file.

要将文本保存到文件,请使用redirection(>)运算符将输出重定向到文件。


$ tr 'a' '1' < input.txt > output.txt

使用tr命令更改文本大小写 (Changing the case of text with tr command)

One of the most common uses of tr command is in translating text from lowercase to uppercase or vice-versa.

tr命令最常见的用途之一是将文本从小写转换为大写,反之亦然。

As tr works on sets of characters, we can explicitly mention the set of lowercase characters as set 1 and set of uppercase characters as set 2 to make the switch.

当tr处理字符集时,我们可以显式地将小写字符集(第1组)和大写字符集(第2组)进行切换。


$ echo "apples and bananas" | tr a-z A-Z

Set a-z represents the set of lower case letters and the set A-Z represents the set of uppercase letters.

集合az代表小写字母的集合,集合AZ代表大写字母的集合。

Another way of doing the same is :

另一种方法是:


$ echo "apples and bananas" | tr [:lower:] [:upper:]

Here, [:lower:] represents the set of lowercase alphabets and [:upper:] represents the set of uppercase alphabets.

在这里, [:lower:]代表小写字母的集合, [:upper:]代表大写字母的集合。

lowercase to Uppercase

用tr删除字符 (Deleting characters with tr)

tr has the ability to delete a set of characters from the text. This is achieved by using tr along with -d command.

tr可以从文本中删除一组字符。 这是通过将tr与-d命令一起使用来实现的。


$  echo "apples and bananas" | tr -d 'n'

This command will eliminate all occurrences of ‘n’ in the text.

此命令将消除文本中所有出现的“ n”

Tr D Command

To remove occurrences of multiple characters, mention all the characters in single quote.

要删除出现多个字符的情况,请在单引号中提及所有字符。


$  echo "apples and bananas" | tr -d 'na'

This command will remove occurrences of ‘n’ and ‘a’

此命令将删除出现的“ n”“ a”

remove multiple characters using tr

Since tr works on the character level, all individual occurrences of ‘n’ and ‘a’ are removed. It’s easy to be mistaken and think that the command will only remove occurrences of ‘na’ occurring in that sequence. However, that’s not the case.

由于tr在字符级别上起作用,因此将删除所有出现的“ n”“ a” 。 很容易被误认为是该命令只会删除该序列中出现的“ na” 。 但是,事实并非如此。

将多个事件压缩为一个 (Squeeze multiple occurrences into one)

Squeezing multiple occurrences into one can be useful to compress the text. It is often used to remove instances of multiple space between lines.

将多个事件压缩为一个可能有助于压缩文本。 它通常用于删除行之间的多个空格的实例。

-s option is used with tr to squeeze.

-s选项与tr一起使用。


$ echo "apples and bananas" | tr -s 'p'
Squeeze

Multiple occurrences of ‘p’ in apple have been reduced to a single occurrence.

苹果中多次出现的“ p”已减少为一次出现。


$  echo "apples and bananas" | tr -s 'na' '1'
Squeeze Replace

The output of this command is equivalent to that of first replacing occurrences of characters ‘n’ and ‘a‘ with ‘1’, followed by a squeeze operation. To compare look at the second command in the output. The result of the second command is of simple character substitution.

此命令的输出等效于首先将出现的字符'n''a '替换为'1' ,然后进行挤压操作。 要进行比较,请看输出中的第二个命令。 第二个命令的结果是简单的字符替换。

Let’s squeeze all the 1’s in the second command’s output to see if we get the same output as the first.

让我们在第二个命令的输出中压缩所有1,以查看是否获得与第一个相同的输出。

comparing the output of squeeze command.

We get the same output as the first command in the output.

我们得到的输出与输出中的第一个命令相同。

To remove consecutive whitespaces in text use :

要删除文本中的连续空格,请使用:


$  echo "apples    and    bananas" | tr -s " " 
Removing Space

Alternatively [:space:] can be used in place of ” “

或者,可以使用[:space:]代替“”


$ echo "apples    and    bananas" | tr -s [:space:]

从文本中提取数字 (Extracting digits out of text)

To achieve operations where only a particular set of characters need to be preserved. It’s best to use -c option. -c is used for complementing the set.

要实现仅需要保留特定字符集的操作。 最好使用-c选项。 -c用于补充集合。

Complement of a set means everything else other than what’s in that set.

集合的补码表示该集合中没有的所有其他内容。


$ echo " Home : 011 1234 4321" | tr -cd [:digit:],'\n' 
Extracting Digits 1

Mentioning ‘\n’ (newline) is important as otherwise the output doesn’t have a newline and gets mixed-up with the next line in the terminal. Another reason to not ignore newlines while deleting characters is that your file could have multiple digits in multiple lines. If the newline character is deleted then all the numbers will appear together without any space.

提及“ \ n”(换行符)很重要,因为否则输出将没有换行符,并且会与终端中的下一行混淆。 删除字符时不忽略换行符的另一个原因是,您的文件可能在多行中有多个数字。 如果删除了换行符,则所有数字将一起显示,没有任何空格。

output without newline
without ‘\n’
没有'\ n'

从文本中提取单词 (Extracting words out of text)

This process is the exact opposite of the one performed above. Here we will ignore the digits and focus only on words made up of letters.

此过程与上述过程完全相反。 在这里,我们将忽略数字,仅关注字母组成的单词。


$ echo " Home : 011 1234 4321" | tr -d [:digit:]
Extracting Words

In this example we have simply deleted all the digits from out text.

在此示例中,我们仅删除了文本外的所有数字。

A more controlled way to do the same would be through complement.

更好的控制方法是通过补码。


$ echo " Home : 011 1234 4321" | tr -cd [:alpha:],'\n'
Removing Digits

[:alpha:] represents the set of alphabets. Think of it as a collection of the two sets, lower and upper.

[:alpha:]代表一组字母。 可以将它视为上下两个集合的集合。


[:alpha:] = [:lower:] + [:upper:]

计算单词出现的次数 (Counting number of occurrences of words)

Counting how many times a word appears in a text can be useful to build histograms. It is also very useful in building probabilistic models for email spam detection.

计算单词在文本中出现的次数对于构建直方图很有用。 在建立概率模型以检测电子邮件垃圾邮件时,它也非常有用。

First, let’s create a file with some recurring words.

首先,让我们创建一个包含一些重复单词的文件。

Cat File

Sometimes it can be useful to display each word of the text in a new line.

有时在换行中显示文本的每个单词会很有用。


$ tr -cs "[:alpha:]" "\n" < input.txt
Displaying Each Word In A New Line
The list goes on. Output has been cut short to fit
清单继续。 输出已切短以适合

To get the number of occurrence for each word use:

要获取每个单词的出现次数,请使用:


$ tr -cs "[:alpha:]" "\n" < input.txt | sort | uniq -c
Count Of Words

Sort is used to sort the list lexicographically. uniq -c counts the individual occurrences of each word and outputs the result as a list of words with a count.

Sort用于字典顺序对列表进行排序。 uniq -c计算每个单词的单个出现次数,并将结果输出为带有计数的单词列表。

结论 (Conclusion)

tr command is useful for performing character-based translations. When combined with other commands like sort or uniq, tr command can turn out to be very powerful. Read more about tr command on its man page. When applying transformations over an entire line, sed command can be used.

tr命令对于执行基于字符的翻译很有用。 与sort或uniq等其他命令结合使用时,tr命令可能会非常强大。 在其手册页上了解有关tr命令的更多信息。 在整行上应用转换时,可以使用sed命令。

翻译自: https://www.journaldev.com/41355/tr-command-in-linux

linux tr 命令

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值