linux rev命令_如何在Linux上使用rev命令

linux rev命令

linux rev命令

Linux terminal on a Ubuntu laptop.
Fatmawati Achmad Zaenuri/Shutterstock Fatmawati Achmad Zaenuri / Shutterstock

Linux’s rev command reverses strings of text. This command can operate either on provided text or a file, and it seems deceptively simple. But like many command-line utilities, its real power becomes apparent when you combine it with other commands.

Linux的rev命令可反转文本字符串。 该命令可以对提供的文本或文件进行操作,这看似简单。 但是,像许多命令行实用程序一样,当您将其与其他命令结合使用时,它的真正功能将变得显而易见。

The rev command is one of those simple Linux utilities that, at first glance, appears to be something of an oddity. It performs a single function: it reverses strings. And apart from being able to print a quick help page (-h) and show you its version number (-V), it doesn’t accept any command-line options.

rev命令是那些简单的Linux实用程序之一,乍一看似乎有些奇怪。 它执行一个功能:反转字符串。 除了能够打印快速帮助页面( -h )并向您显示其版本号( -V )之外,它不接受任何命令行选项

So, rev reverses strings, and that’s it? No variations or options? Well, yes and no. Yes, it has no permutations, but no, that’s hardly all. This tutorial shows you how to combine it for powerful operations.

那么, rev反转字符串,仅此而已? 没有变化或选择? 好吧,是的,不是。 是的,它没有排列,但不是,仅此而已。 本教程向您展示如何将其组合以进行强大的操作。

When you use rev as a building block in more complicated command sequences, it really starts to show its worth. rev is one of a group of commands (like tac and yes) that are facilitators. It’s easier to appreciate their usefulness when you see how they make the use of other commands more efficient.

当您在更复杂的命令序列中将rev用作构建块时,它实际上开始显示其价值。 rev是辅助命令(例如tacyes )的一组命令之一。 当您了解它们如何使其他命令的使用效率更高时,更容易意识到它们的用处。

使用rev命令 (Using the rev Command)

Used on the command line with no other parameters, rev takes any typed input, reverses it, and then prints it in the terminal window. It keeps doing this until you hit Ctrl+C to exit.

在命令行上不带其他参数使用, rev接受任何类型的输入,将其反转,然后在终端窗口中打印。 它会一直这样做,直到您按Ctrl + C退出为止。

rev
"rev" used with "stdin" in a terminal window.

If you type some text and press Enter, it makes rev print the string in reverse—unless you provide it with a palindrome, of course.

如果键入一些文本并按Enter,它将使rev反向打印该字符串-当然,除非您为其提供回文

"rev" with a palindrome in a terminal window.

将文字传递到转速 (Passing Text to rev)

You can use echo to pipe text to rev.

您可以使用echo将文本传递到rev

echo one two three | rev
"echo one two three | rev" in a terminal window.

You can also use rev to reverse the contents of an entire file of text, line by line. In this example, we have a file containing a list of filenames. The file is called “filelist.txt.”

您还可以使用rev逐行逆转整个文本文件的内容。 在此示例中,我们有一个包含文件名列表的文件。 该文件称为“ filelist.txt”。

rev filelist.txt
"rev filelist.txt" in a terminal window.

Each line is read from the file, reversed, and then printed to the terminal window.

从文件中读取每一行,取反,然后打印到终端窗口。

将rev与其他命令结合 (Combining rev with Other Commands)

Here’s an example using piping of input that calls rev twice.

这是一个使用输入管道调用两次rev的示例。

This command strips the last character off the string of text. This could be useful to remove punctuation. We need to use the cut command to strip the character.

此命令从文本字符串中去除最后一个字符。 这对于删除标点符号可能很有用。 我们需要使用cut命令剥离字符

echo 'Remove punctuation.' | rev | cut -c 2- | rev
"echo 'Remove punctuation.' | rev | cut -c 2- | rev" in a terminal window.

Let’s break that down.

让我们分解一下。

  • echo sends the string into the first call to rev.

    echo将字符串发送到对rev的第一个调用中。

  • rev reverses the string and pipes it into cut.

    rev反转字符串并将其通过管道传递到cut

  • The -c (characters) option tells cut to return a sequence of characters from the string.

    -c (字符)选项告诉cut返回字符串中的字符序列。

  • The 2- option tells cut to return the range of characters from character two until the end of the line. If a second number were provided, such as 2-5, the range would be from characters two to five. No second number means “up to the end of the string.”

    2-选项告诉cut返回从字符2到行尾的字符范围。 如果提供了第二个数字,例如2-5 ,则范围是从两个字符到五个字符。 没有第二个数字表示“直到字符串的末尾”。

  • The reversed string—minus its first character—is passed to rev which reverses the string, so it’s back to its original order.

    反转的字符串(减去其第一个字符)将传递给rev ,后者会将字符串反转,因此它又恢复了其原始顺序。

Because we trimmed off the first character of the reversed string, we trimmed off the last character of the original string. Yes, you could do this with sed or awk, but this is an easier syntax.

因为我们修剪掉了反向字符串的第一个字符,所以我们修剪了原始字符串的最后一个字符。 是的,您可以使用sedawk做到这一点,但这是一个更简单的语法。

分开最后的话 (Separating the Last Word)

We can use a similar trick to return the last word of the line.

我们可以使用类似的技巧来返回该行的最后一个单词。

The command is similar to the last one: again, it uses rev twice. The differences lie in the way the cut command is used to select portions of the text.

该命令与上一个命令相似:再次,它两次使用rev 。 区别在于cut命令用于选择文本部分的方式。

echo 'Separate the last word' | rev | cut -d' ' -f1 | rev
"echo 'Separate the last word' | rev | cut -d' ' -f1 | rev" in a terminal window.

Here’s the command breakdown:

以下是命令分解:

  • echo sends the string into the first call to rev.

    echo将字符串发送到对rev的第一个调用中。

  • rev reverses the string and pipes it into cut.

    rev反转字符串并将其通过管道传递到cut

  • The -d' ' (delimiter) option tells cut to return a sequence of characters delimited by a space.

    -d' ' (定界符)选项告诉cut返回由空格分隔的字符序列。

  • The -f1 option tells cut to return the first section of the string not containing the delimiter. In other words, the first part of the sentence up to the first space.

    -f1选项告诉cut返回不包含定界符的字符串的第一部分。 换句话说,句子的第一部分最多到第一空格。

  • The reversed first word is passed to rev which reverses the string, so it’s back to its original order.

    反转的第一个单词传递给rev ,后者反转字符串,因此它恢复了其原始顺序。

Because we extracted the first word of the reversed string, we trimmed off the last word of the original string. The last word of the sentence was “word,” and it’s printed out for us.

因为我们提取了反向字符串的第一个单词,所以我们修剪掉了原始字符串的最后一个单词。 句子的最后一个单词是“单词”,它已为我们打印出来。

从文件整理内容 (Trimming Content From Files)

Let’s say we have a file containing a list of filenames, and the filenames are in quotation marks. We want to remove the quotation marks from the filenames.

假设我们有一个包含文件名列表的文件,文件名用引号引起来。 我们要从文件名中删除引号。

Let’s look at the file:

让我们看一下文件:

less filelist.txt
"less filenames.txt" in a terminal window.

The contents of the file are displayed for us in less.

该文件的内容在less为我们显示。

Contents of filenames.txt in less in a terminal window.

We can remove the punctuation from both ends of each line with the following command. This command uses both rev and cut twice.

我们可以使用以下命令从每行的两端删除标点符号。 此命令同时使用revcut两次。

rev filelist.txt | cut -c 2- | rev | cut -c 2-
"rev filelist.txt | cut -c 2- | rev | cut -c 2-" in a terminal window.

The filenames are listed for us without the quotation marks.

列出的文件名不带引号。

Filenames without quotation marks in a terminal window.

The command breaks down like this:

该命令分解如下:

  • rev reverses the lines in the file and pipes them into cut.

    rev反转文件中的行并将它们通过管道传递到cut

  • The -c (characters) option tells cut to return a sequence of characters from each line.

    -c (字符)选项告诉cut从每行返回一个字符序列。

  • The 2- option tells cut to return the range of characters from character two until the end of each line.

    2-选项告诉cut返回从字符2到每行结尾的字符范围。

  • The reversed strings, minus their first characters, are passed to rev.

    反向的字符串减去它们的第一个字符,将传递给rev.

  • rev reverses the strings, so they’re back to their original order. They’re piped into cut a second time.

    rev反转字符串,因此它们恢复了原始顺序。 它们被第二次cut管道。

  • The -c (characters) option tells cut to return a sequence of characters from each string.

    -c (字符)选项告诉cut从每个字符串返回一个字符序列。

  • The 2- option tells cut to return the range of characters from character two until the end of each line. This “hops over” the leading quotation mark, which is character one on each line.

    2-选项告诉cut返回从字符2到每行结尾的字符范围。 这“跳过”了引号,它是每一行的第一个字符。

大量管道 (A Lot of Piping)

Here’s a command that returns a sorted list of every file extension in the current directory. It uses five distinct Linux commands.

这是一条返回当前目录中每个文件扩展名的排序列表的命令。 它使用五个不同的Linux命令。

ls | rev | cut -d'.' -f1 | rev | sort | uniq
"ls | rev | cut -d'.' -f1 | rev | sort | uniq" in a terminal window.

The process is straightforward:

这个过程很简单:

  • ls lists the files in the current directory. These are piped into rev.

    ls列出当前目录中的文件。 这些通过管道传递到rev

  • rev reverses the filenames and pipes them into cut.

    rev反转文件名并将它们通过管道传递到cut

  • cut returns the first portion of each filename up to a delimiter. The -d'.' tells cut to use the period “.” as the delimiter. The portion of the reversed filenames up to the first period are the file extensions. These are piped into rev.

    cut返回每个文件名的第一部分,直到一个定界符。 -d'.' 告诉cut使用句点“。” 作为分隔符。 直到第一个期间的反向文件名的部分是文件扩展名。 这些通过管道传递到rev

  • rev reverses the file extensions into their original order. They are piped into sort.

    rev将文件扩展名恢复为原始顺序。 他们被管道输送到sort

  • sort sorts the file extensions and pipes the results into uniq.

    sort对文件扩展名sort排序,并将结果通过管道uniquniq

  • uniq returns a single listing for each type of unique file extension. Note if there’s no file extension (such as for the makefile, and the directories Help and gc_help), the entire filename is listed.

    uniq为每种类型的唯一文件扩展名返回一个清单。 请注意,如果没有文件扩展名(例如,makefile以及目录Help和gc_help),则会列出整个文件名。

To put a finishing touch to it, add the -c (count) command-line option to the uniq command.

要对其进行最后uniq ,请在uniq命令中添加-c (计数)命令行选项。

ls | rev | cut -d'.' -f1 | rev | sort | uniq -c
"ls | rev | cut -d'.' -f1 | rev | sort | uniq -c" in a terminal window.

We now get a sorted list of the different file types in the current directory with a count of each.

现在,我们在当前目录中获得了不同文件类型的排序列表,每个都有一个计数。

That’s a pretty nifty one-liner!

那是一个很漂亮的单线!

抽屉和抽屉 (drawroF og ot drawkcaB gnioG)

Sometimes you have to go backward to go forward. And you usually go forward fastest as part of a team.

有时您必须后退才能前进。 通常,您作为团队的一员前进得最快。

Add rev to your repertoire of go-to commands, and you’ll soon be using it to simplify otherwise complicated command sequences.

rev添加到您的go-to命令库中,您将很快使用它来简化原本复杂的命令序列。

翻译自: https://www.howtogeek.com/434180/how-to-use-the-rev-command-on-linux/

linux rev命令

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值