Using the Linux look command to select lines from files

  • Share on Facebook
  • Share on Twitter
  • Share on LinkedIn
  • 分享Reddit
  • Share by Email
  • Print resource
man with binoculars future it vision horizon by marvod getty
marvod / Getty Images

Thelookcommand on Linux can be handy for selecting particular lines from text files with sorted contents. Let's look into how it can be used and where you might run into some problems.

Case sensitivity

如果您键入诸如“ Look Unix”之类的命令,则应看到以下内容:

$ Look Unix Unix Unix Unix

Notice that, because no file was specified in the command shown,look恢复使用words在系统上文件(可能/usr/share/dict/单词或任何指向的)。还要注意,即使命令的参数仅具有小写字符,它也会找到文件中的三行。当您不提供文件名时,该命令是不敏感的,而是允许其默认为words文件。

For a word like "snow", you might get a couple hundred responses. Here's just the beginning of what you should see:

$看雪|列|头-5雪雪堆,雪雪雪在山上的雪球雪球雪花雪花雪花雪球雪雪雪堆雪球雪花雪花

将输出管输送到columnprovides a multi-column display. In the output above, we use thecommand to show only the first five lines.

Specifying the file to use

To pull lines from an arbitrary text file, add its name to thelookcommand like this:

$ look 2 myfile 2) I don't like my file

In that command,lookselects lines from the file that begin with "2". To be a little more specific, you could do this:

$ look "2)" myfile 2) I don't like my file

在这种情况下,需要搜索词的引号,因为括号否则可以解释为定界符。

If you are looking for content in some file other than thewordsfile, you will also likely need to include a-F(or--ignore-case) option to view contents regardless of case. As mentioned earlier, while thelookcommand is case-insensitive when using thewordsfile, this isn't so when specifying some other file, so you may need the option to make it do what you want. In the example below, you can see how much difference this can make.

$ look k shopping_list kiwifruit $ look -f k shopping_list kiwifruit Klondike bars

这很重要,要记住。毕竟,您不想冒险忘记那些克朗代克酒吧!

The look command expects files to be sorted

重要的是要记住lookcommand expects files to be sorted alphanumerically. Let's say we were trying to extract content from a file with this content:

$ cat nums 1 2 3 4 6 5

如果我们问lookto select the line with the "4", all is well.

$ look 4 nums 4

如果我们问lookto select the line beginning with a "5", on the other hand, it doesn't comply because the 5 appears following a 6. In other words, it may stop searching when it encounters an error in the sort order.

$ look 5 nums $ <== no response

You also can't simply sort a file and pipe its contents to thelookcommand. While we may not see any obvious errors in the output shown below,look忽略管道并使用默认wordsfile instead of the "nums" file. Piping just isn't welcome.

$ stort nums |看5个5点5-T 5th

It's no accident thislook命令给出相同的结果:

$ look 5 5-point 5-T 5th

替代方案look

格雷普command (an excellent choice for searching for lines containing any specified text) will generally look for the text that you specify anywhere in the file, it can be constrained to looking only for lines that begin with that text by using the^character.

$ grep ^unix/usr/share/dict/单词unix

自从格雷普isn't case-insensitive by default, add the-我(ignore case) when you want to select content regardless of case.

$ grep -i ^Unix /usr/share/dict/words UNIX Unix unix

The格雷普command also doesn't care if the file content is sorted or not.

Wrap-Up

Thelookcommand can be handy, especially when you want to find words that begin with certain characters in thewordsfile or other sorted files. It's somewhat tedious if you have to sort your file contents first and save the sorted contents to a second file before you conduct your search. In that case,格雷普is a much easier choice and will save you some time.

Thelookcommand's primary strength is finding all the words in thewordsfile that begin with a particular string.

$ look easy | column easy easygoing easy-humored easy-natured easy-spoken easy-fitting easygoingly easylike easy-paced easy-flowing easygoingness easy-mannered easy-rising easy-going easy-hearted easy-minded easy-running
Related:

版权所有©2022 IDG Com足球竞彩网下载munications,Inc。

The 10 most powerful companies in enterprise networking 2022