天天看点

在 Unix 系统上查找数据的最佳工具和技巧

有时候在 unix 系统上查找信息就如同大海捞针。如果重要的信息被淹没在大量文本中,它们也很难被注意到。目前我们中的很多人都在处理“大数据” —— 从数十亿字节大小的日志文件和巨大的各种格式记录集合中挖掘商业情报。

幸运的是,只有在两种情况下,你才需要在成堆的数据中挖掘,继而完成你的工作 —— 当你知道你要找什么和当你不知道的时候。:) 最佳工具和技巧取决于你面临两种情况中的哪一种。

在 Unix 系统上查找数据的最佳工具和技巧

<a target="_blank"></a>

当你知道你要找什么,grep 就是你的朋友,这不只是在你查找特定文本的时候。grep 命令可以帮助你找到任意文本,特定单词,文本模式和有上下文的文本。当你知道文本长什么样时,查找它通常很简单。grep this that 命令会显示“that”文件中包含“this”字符串的每一行。增加 -w 选项就只会显示那些单独包含“this”这个单词的行。换句话说,如果行中包含“thistle” 或 “erethism” 就不会显出来,除非这些行也有 “this” 这个单词。

最简单的 grep 命令不费什么力气就能理解:

<code>$ grep find poem</code>

<code>finding meaning, finding comfort,</code>

<code>finding someone to adore</code>

<code>can we find a way to be</code>

查找整个单词可以通过增加 -w 选项完成:

<code>$ grep -w find poem</code>

查找模式需要一点技巧。我们的第一个例子中显示了包含“find”单词的行,无论“find”中的“f”是大写还是小写:

<code>$ grep [ff]ind poem</code>

<code>finding answers</code>

如果你想匹配以文本起始或结束的行,你可以使用 ^(起始)或 $(结尾)。

<code>$ grep ^find poem</code>

如果你想找到包含两个连续元音音节的单词的行,你可以使用如下所示的“aeiouaeiou”字符。

<code>$ grep -e "[aeiouaeiou]{2}" poem | head -3</code>

<code>all our days are filled with searching</code>

<code>wondering what we're looking for</code>

查找包含 9 个或者 10 个字母的字符串:

<code>$ grep -e "[[:alpha:]]{9,10}" poem</code>

<code>that makes the searching more productive</code>

查找一个包含 “find” 的长单词:

<code>$ grep -e "find[^[:space:]]+" poem</code>

我们中的大多数人不会去查找诗歌,这是显而易见的,但我们可以使用同样的技巧来从我们的系统文件中获取相关的信息。在下面的例子里,我们查找”processor”这个术语,并且按照五行一组(前置两行后置两行)显示出来以便提供一些上下文。如果你希望得到 9 行一组,将 -c 2 变成 -c 4 就可以了。

<code>$ grep -c 2 processor /var/log/dmesg</code>

<code>using acpi (madt) for smp configuration information</code>

<code>allocating pci resources starting at 88000000 (gap: 80000000:7ec00000)</code>

<code>detected 3400.426 mhz processor.</code>

<code>built 1 zonelists. total pages: 524275</code>

<code>kernel command line: ro root=label=/1</code>

<code>--</code>

<code>inode-cache hash table entries: 65536 (order: 6, 262144 bytes)</code>

<code>memory: 2071140k/2097100k available (2223k kernel code, 24616k reserved, 922k data, 232k init, 1179596k highmem)</code>

<code>checking if this processor honours the wp bit even in supervisor mode... ok.</code>

<code>calibrating delay loop (skipped), value calculated using timer frequency.. 6800.85 bogomips (lpj=3400426)</code>

<code>security framework v1.0.0 initialized</code>

<code>cpu0: intel(r) xeon(tm) cpu 3.40ghz stepping 04</code>

<code>smp alternatives: switching to smp code</code>

<code>booting processor 1/1 eip 11000</code>

<code>cpu 1 irqstacks, hard=c0779000 soft=c0759000</code>

<code>initializing cpu</code>

<code>#1</code>

<code>cpu1: intel(r) xeon(tm) cpu 3.40ghz stepping 04</code>

<code>booting processor 2/6 eip 11000</code>

<code>cpu 2 irqstacks, hard=c077a000 soft=c075a000</code>

<code>#2</code>

<code>cpu2: intel(r) xeon(tm) cpu 3.40ghz stepping 04</code>

<code>booting processor 3/7 eip 11000</code>

<code>cpu 3 irqstacks, hard=c077b000 soft=c075b000</code>

<code>#3</code>

如果你要查找一个已知位置的文本,例如当 perl 告诉你脚本执行到第 73 行出现了问题,或者你正在处理文件的第 1892 行,你可以使用sed 来显示特定的行(我只是不喜欢数到 1892 行)。而且额外花一点点力气,你就可以只显示这一行。

错误信息可能像这个样子:

<code>“syntax error line 73 near ”} else“ ”</code>

你可以使用一个sed命令来显示出问题的这行:

<code>$ sed -n 73p showvars</code>

<code>else</code>

好了,就是这行,但是我们也没有比之前多知道些什么。通过显示前面几行可以增加一点上下文信息,我们就可以定位错误。这里有一个类似的命令可以显示这行和之前的十行:

<code>$ sed -n 63,73p showvars</code>

<code>if $password eq "a_secret";</code>

<code>{</code>

<code>foreach $var (sort(keys(%env))) {</code>

<code>$val = $env{$var};</code>

<code>$val =~ s|n|n|g;</code>

<code>$val =~ s|"|"|g;</code>

<code>print '${var}="${val}"n'</code>

<code>};</code>

<code>}</code>

哎呦!这看上去是某些人在写 if 语句时出了问题!我们可以很容易地修复它。

你还可以使用 sed 命令来强调包含特定内容的行。在下面的命令里,我们增加了一个 “箭头标记” 来强调每一个包含 foreach 命令的行:

<code>$ sed '/print/{b label1; {:label1 ; s/^/# / ; s/$/ &lt;===/ ;} }' showvars</code>

<code>#!/bin/bash</code>

<code></code>

<code># print '${var}="${val}"n' &lt;===</code>

你可以使用类似的命令注释掉你的 print 命令:

大海捞针很难,其实地毯上找针也都不容易。但是通过使用一些最常见 unix 命令的变形,就可以很容易找到你要找的东西,甚至当你并不知道要找什么的时候。

本文来自云栖社区合作伙伴“linux中国”,原文发布日期:2015-10-19