每个开发人员到了他们职业人生的某个阶段的时候,将会发现自己要寻找有关Linux的信息。我并不是这方面的专家。但是掌握了以下8个命令,我几乎可以得到我任何需要的东西。 注意:以下的命令都有很多扩展的文档,博客里提出的知识我最常用的命令,用法。如果你不了解Linux命令,这个帖子会给你一点指导。 我们以一些文本举例。假设我们有2个文件,里面有订单关于第三方的放置地点和发送回应。 order.out.log 8:22:19 111, 1, Patterns of Enterprise Architecture, Kindle edition, 39.99 8:23:45 112, 1, Joy of Clojure, Hardcover, 29.99 8:24:19 113, -1, Patterns of Enterprise Architecture, Kindle edition, 39.99 order.in.log 8:22:20 111, Order Complete 8:23:50 112, Order sent to fulfillment 8:24:20 113, Refund sent to processing cat http://linux.about.com/od/commands/l/blcmdl1_cat.htm –追加文件并在标准输出上打印 jfields$ cat order.out.log 8:22:19 111, 1, Patterns of Enterprise Architecture, Kindle edition, 39.99 8:23:45 112, 1, Joy of Clojure, Hardcover, 29.99 8:24:19 113, -1, Patterns of Enterprise Architecture, Kindle edition, 39.99
正如他的名字所说的,你可以串联多个文件 jfields$ cat order.* 8:22:20 111, Order Complete 8:23:50 112, Order sent to fulfillment 8:24:20 113, Refund sent to processing 8:22:19 111, 1, Patterns of Enterprise Architecture, Kindle edition, 39.99 8:23:45 112, 1, Joy of Clojure, Hardcover, 29.99 8:24:19 113, -1, Patterns of Enterprise Architecture, Kindle edition, 39.99
看到效果了,但我们可以提高其可读性。 sort http://linux.about.com/library/cmd/blcmdl1_sort.htm –对文本文件进行行排序,这里使用排序是不错的选择 jfields$ cat order.* | sort 8:22:19 111, 1, Patterns of Enterprise Architecture, Kindle edition, 39.99 8:22:20 111, Order Complete 8:23:45 112, 1, Joy of Clojure, Hardcover, 29.99 8:23:50 112, Order sent to fulfillment 8:24:19 113, -1, Patterns of Enterprise Architecture, Kindle edition, 39.99 8:24:20 113, Refund sent to processing
上面显示了我们想要看到的效果,但是这只是小文件。而真实的数据是很大的,有些是你不想要的数据怎么办? grep http://linux.about.com/od/commands/l/blcmdl1_grep.htm grep, egrep, fgrep–进行匹配输出 假设我只关心给PofEAA的订单,使用grep就可以做到。 jfields$ cat order.* | sort | grep Patterns 8:22:19 111, 1, Patterns of Enterprise Architecture, Kindle edition, 39.99 8:24:19 113, -1, Patterns of Enterprise Architecture, Kindle edition, 39.99
假设订单113里面发生了一些问题,你想看到关于113的所有订单信息。没错,grep能帮你。 jfields$ cat order.* | sort | grep “:\d\d 113, ” 8:24:19 113, -1, Patterns of Enterprise Architecture, Kindle edition, 39.99 8:24:20 113, Refund sent to processing
你会发现在表达式里面不止有113,这是因为113也可能出现在价格里面,或者产品里面,这样做是严格限制其查找结果。 现在我们已经发出退货订单的信息,我们每日也要给会计发送销售统计。他们要求每个PofEAA的项目,但他们只关心数量和价格,我们要把 不需要的部分删减掉。 cut http://linux.about.com/od/commands/l/blcmdl1_cut.htm –从文件的每一行删除一部分 还是要先使用grep。 jfields$ cat order.* | sort | grep Patterns 8:22:19 111, 1, Patterns of Enterprise Architecture, Kindle edition, 39.99 8:24:19 113, -1, Patterns of Enterprise Architecture, Kindle edition, 39.99 jfields$ cat order.* | sort | grep Patterns | cut -d”,” -f2,5 1, 39.99 -1, 39.99
我们已经减少了数据,让会计一目了然。 假设会计想要把订单ID做为参考,把它放在每一行的最后,并用单引号。 sed http://linux.about.com/od/commands/l/blcmdl1_sed.htm –流编辑器。用来处理文本转换。 下面的示例演示怎样使用它来做到我们想要的数据。 jfields$ cat order.* | sort | grep Patterns \ >| sed s/”[0-9\:]* \([0-9]*\)\, \(.*\)”/”\2, ‘\1′”/ 1, Patterns of Enterprise Architecture, Kindle edition, 39.99, ’111′ -1, Patterns of Enterprise Architecture, Kindle edition, 39.99, ’113′ lmp-jfields01:~ jfields$ cat order.* | sort | grep Patterns \ >| sed s/”[0-9\:]* \([0-9]*\)\, \(.*\)”/”\2, ‘\1′”/ | cut -d”,” -f1,4,5 1, 39.99, ’111′ -1, 39.99, ’113′
这是一个正则表达式,但没什么复杂的。做以下事情 1.删除时间 2.捕获订单号 3.删除逗号和订单号后面的空格 4.捕获此行的其余部分 一旦我们看到了我们需要的数据,可以使用\1&\2让输出数据符合我们的格式要求。 uniq http://linux.about.com/library/cmd/blcmdl_uniq.htm –去除重复行 下面的示例演示如何grep的唯一相关的交易,削减不必要的信息,并获得计数。 jfields$ cat order.out.log | grep “\(Kindle\|Hardcover\)” | cut -d”,” -f3 | sort | uniq -c 1 Joy of Clojure 2 Patterns of Enterprise Architecture jfields$ cat order.out.log | grep “\(Kindle\|Hardcover\)” | cut -d”,” -f3 | sort | uniq Joy of Clojure Patterns of Enterprise Architecture
find http://linux.about.com/od/commands/l/blcmdl1_find.htm –在目录里找文件 假设这2个文本文件存在于我们的主目录,我们不必知道他们的全名。 jfields$ find /Users -name “order*” Users/jfields/order.in.log Users/jfields/order.out.log
当然还有很多选项,但99%的情况下我这么做。 less http://linux.about.com/library/cmd/blcmdl1_less.htm –在一个文件里面向前向后移动 让我们回到最简单的cat|sort的例子。你可以向前搜索使用”/”,向后使用”?”,2者都可以使用正则表达式。 jfields$ cat order* | sort | less 你可以试试/113.*,这将突出显示订单113。你可以使用?.*112,也将突出显示订单112,你可以用’q'退出。 Linux命令很丰富,有些人很头疼。这几个命令应该能帮你完成大部分的文本工作,不用交到你的脚本语言手里。 原文:8 Linux Commands Every Developer Should Know 译文:http://www.daidata.com/archives/1635 |