如何在Linux上使用look命令

Linux terminal running on an Ubuntu-themed desktop environment.
Fatmawati Achmad Zaenuri/Shutterstock Fatmawati Achmad Zaenuri / Shutterstock

The Linux look command races through a file and lists all the lines that begin with a particular word or phrase. But watch out! It behaves differently on different Linux distributions. This tutorial will show you how to use it.

Linux look命令遍历文件并列出以特定单词或短语开头的所有行。 但是要当心! 在不同Linux发行版上,它的行为有所不同。 本教程将向您展示如何使用它。

Ubuntu的外观命令行为不同 (Ubuntu’s look Command Behaves Differently)

For a simple, but useful, command, look certainly gave me the runaround when I was researching this article. There were two problems: compatibility and documentation.

对于一个简单但有用的命令,当我研究本文时, look肯定给了我解决方法。 有两个问题:兼容性和文档。

This article was checked using Ubuntu, Fedora, and Manjaro. look was bundled with each of those distributions, which was great. The problem was the behavior wasn’t the same across all three. The Ubuntu version was very different. According to the Ubuntu Manpages, the behavior should be the same.

本文已使用Ubuntu,Fedora和Manjaro进行了检查。 look与每个发行版捆绑在一起,非常好。 问题在于这三个行为都不相同。 Ubuntu版本是非常不同的。 根据Ubuntu手册页 ,行为应相同。

I eventually figured it out. look traditionally uses a binary search, while Ubuntu look uses a linear search. The online Ubuntu man pages for Bionic Beaver (18.04), Cosmic Cuttlefish (18.10), and Disco Dingo (19.04) all say the Ubuntu version uses a binary search, which is not the case.

我最终想通了。 look传统使用二进制搜索 ,而Ubuntu的look采用的是线性搜索 。 针对Bionic Beaver(18.04),Cosmic Cuttlefish(18.10)和Disco Dingo(19.04)的在线Ubuntu手册页都说,Ubuntu版本使用二进制搜索,事实并非如此。

If we take a look at the local Ubuntu man page, we see it clearly states their look uses a linear search. There is a command-line option to force it to use a binary search. Neither of the versions in the other distributions has an option to choose between search methods.

如果我们看一下本地的 Ubuntu手册页,就会清楚地看到它们的look使用线性搜索。 有一个命令行选项可以强制它使用二进制搜索。 其他发行版中的两个版本均未提供在搜索方法之间进行选择的选项。

man look
"man look" in a terminal window.

Scrolling down through the man page, we see the section that describes this version of  look using a linear instead of binary search.

向下滚动手册页,我们看到使用线性搜索而不是二进制搜索描述此版本look的部分。

Ubuntu look man page in a terminal window.

The moral of the story is to check the local man pages first.

这个故事的寓意是首先检查当地的手册页。

线性搜索与二进制搜索 (Linear Search versus Binary Search)

The binary search method is faster and more efficient than a linear search. Working with large files makes this very apparent. The downside to the binary search is your file must be sorted. If you don’t want to sort your file, sort a copy of it, and then use that with look.

二进制搜索方法比线性搜索更快,更高效。 使用大文件使这一点很明显。 二进制搜索的缺点是您的文件必须排序。 如果您不想对文件进行排序,请对文件的副本进行排序,然后将其与look一起使用。

We’ll demonstrate this elsewhere in this article. Just be aware that on Fedora, Manjaro, and I expect most of the rest of the Linux world, you’ll need to create a sorted copy of your file and work with that.

我们将在本文的其他地方进行演示。 请注意,在Fedora,Manjaro上,我希望在Linux世界的大多数其他地方,您需要创建文件的排序副本并进行处理。

安装文字 (Installing words)

look can work with any text file you choose, or it can work with the local dictionary file “words.”

look可以与您选择的任何文本文件一起使用,也可以与本地词典文件“ words”一起使用。

On Manjaro you need to install the “words” file. Use this command:

在Manjaro上,您需要安装“ words”文件。 使用此命令:

sudo pacman -Syu words
"sudo pacman -Syu words" in a terminal window.

使用外观 (Using look)

For this article, we’ll work with a text file of the Edward Lear poem “The Jumblies.”

对于本文,我们将使用Edward Lear诗作“ The Jumblies”的文本文件。

Let’s look at its contents with this command:

让我们用以下命令查看其内容:

less the-jumblies.txt
"less the-jumblies.txt" in a terminal window.

Here’s the first part of the poem. Note that we’re using Ubuntu, so the file remains unsorted. For Fedora and Manjaro, we’d work with a sorted copy of the file, which we’ll cover later in this article.

这是这首诗的第一部分。 请注意,我们使用的是Ubuntu,因此文件未排序。 对于Fedora和Manjaro,我们将使用文件的排序副本,我们将在本文后面介绍。

"The Jumblies" in less in a terminal window.

If we look for lines that start with the word, “They,” we’ll find out some of what the Jumblies did.

如果我们寻找以“他们”一词开头的行,我们会发现Jumblies所做的一些事情。

look They the-jumblies.txt
"look They the-jumblies.txt" in a terminal window.

look responds by listing these lines:

look通过列出以下行look响应:

Output from "look They the-jumblies.txt" in a terminal window.

忽略字符大小写 (Ignoring Character Case)

To make look ignore differences between upper- and lowercase, use the -f (ignore case) option. We’ve used “they” as the search word again, but this time, it’s in lowercase.

要使look忽略大写和小写之间的差异,请使用-f (忽略大小写)选项。 我们再次使用“他们”作为搜索词,但是这次是小写。

look -f they the-jumblies.txt
look -f they the-jumblies.txt in a terminal window.

This time, the results include an extra line.

这次,结果包括额外的一行。

Output from "look -f they the-jumblies.txt" in a terminal window.

The line that begins with “THEY” was missed in the last set of results because it’s in all uppercase and didn’t match our search term, “They.”

在最后一组结果中漏掉了以“ THEY”开头的行,因为该行全为大写,与我们的搜索词“他们”不匹配。

Ignoring case allows look to include it in the results.

忽略大小写允许look将其包括在结果中。

将外观与已排序文件一起使用 (Using look with a Sorted File)

If your Linux distribution has a version of look that follows the traditional behavior of using a binary search, you must either sort your file or work with a sorted copy of it.

如果您Linux发行版的look符合使用二进制搜索的传统行为,则必须对文件进行排序或使用文件的排序副本。

Let’s repeat the command to search for “They,” but this time on Manjaro.

让我们重复命令以搜索“它们”,但这一次是在Manjaro上。


   
   
look They the-jumblies.txt in a terminal window.

As you can see, no results were returned. But we know there are lines in the poem that start with the word, “They.”

如您所见,没有结果返回。 但是我们知道这首诗中有一些词以“他们”开头。

Let’s make a sorted copy of the file. If you’re going to use the -f (ignore case) or -d (alphanumeric characters and spaces only) options with look, you must use them when you sort the file.

让我们对文件进行排序后的副本。 如果将-f (忽略大小写)或-d (仅字母数字字符和空格)选项与look使用,则在对文件进行排序时必须使用它们。

The -o (output) option allows you to specify the name of the file the sorted lines should be added to. In this example, it’s “sorted.txt.”

-o (输出)选项使您可以指定应将已排序行添加到的文件的名称。 在此示例中,为“ sorted.txt”。

sort -f -d the-jumblies.txt -o sorted.txt
sort -f -d the-jumblies.txt -o sorted.txt in a terminal window.

Let’s use look on the sorted.txt file, and then use the -f and -d options.

让我们用look就sorted.txt文件,然后使用-f-d选项。

sort -f -d the-jumblies.txt -o sorted.txt in a terminal window.

Now, we get the results we expected.

现在,我们得到了预期的结果。

只考虑空格和字母数字 (Only Consider Spaces and Alphanumerics)

To make look ignore anything that isn’t an alphanumeric character or a space, use the -d (alphanumeric) option.

要使外观忽略任何不是字母数字字符或空格的内容,请使用-d (字母数字)选项。

Let’s see if there are any words that start with, “Oh.”

让我们看看是否有任何以“哦”开头的单词。

look -f oh the-jumblies.txt
look -f oh the-jumblies.txt in a terminal window.

No results are returned by look.

look没有返回任何结果。

Let’s try again and tell look to ignore anything other than alphanumeric characters and spaces. That means characters and symbols, such as punctuation, will be ignored.

让我们再试一次,告诉外观忽略字母数字字符和空格以外的任何内容。 这意味着字符和符号(例如标点符号)将被忽略。

look -f -d oh the-jumblies.txt
look -f -d oh the-jumblies.txt in a terminal window.

This time, we get a result. We didn’t find this line before because the quotation marks and exclamation point confused the search.

这次,我们得到了结果。 我们之前找不到这行是因为引号和感叹号混淆了搜索。

Output from "look -f -d oh the-jumblies.txt" in a terminal window.

指定终止符 (Specifying the Terminating Character)

You can tell look to use a specific character as the terminating character. Usually, spaces and end of lines are used as the terminating character.

您可以告诉look使用特定字符作为终止字符。 通常,空格和行尾用作终止字符。

The -t (terminate character) option allows us to specify the character we’d like to use. In this example, we’re going to use the apostrophe character. We need to quote it with a backward slash so that look knows we’re not opening a string.

-t (终止字符)选项允许我们指定我们要使用的字符。 在此示例中,我们将使用撇号字符。 我们需要用反斜杠将其引用,以使look知道我们没有打开字符串。

We’re also quoting the search term because it includes a space. We’re searching for two words.

我们还引用了搜索词,因为它包含一个空格。 我们正在搜索两个词。

look -f -t \' "they call" the-jumblies.txt
look -f -t \' "they call" the-jumblies.txt in a terminal window.

The results match the search term, terminated by the apostrophe we used as the terminating character.

结果与搜索词匹配,以我们用作终止符的撇号终止。

Output from "look -f -t \' "they call" the-jumblies.txt" in a terminal window.

在没有文件的情况下使用外观 (Using look Without a File)

If you don’t provide a filename on the command line, look uses the words file.

如果您未在命令行上提供文件名,请使用“文件”一词

The command:

命令:


   
   
"look circle" in a terminal window.

gives these results:

给出以下结果:

"look circle" results in a terminal window.

These are all the words in the file that begin with the word “circle.”

这些都是文件中所有以“ circle”开头的词。

看起来不再 (look No Further)

That’s all there is to look.

这就是所有look

It’s pretty easy once you know there are different behaviors across different Linux distributions, and you’ve bottomed out whether your version uses a binary or linear search.

一旦知道不同Linux发行版之间存在不同的行为,并且已经确定自己的版本使用二进制搜索还是线性搜索,这将非常容易。

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

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值