有关从标准 Linux 文件中提取宝贵数据片段的几点建议 Linux 本文介绍了 grep、cut、 grep 名称 grep 是 Global Regular Expression g/re/p 其中,“re”表示“正则表达式”。自从 UNIX 及其各种同类产品(包括 在了解 grep、fgrep 和 egrep 的功能之前,必须先了解正则表达式的概念及其用法。 使用正则表达式。就其最简 问题是“addition”将更改为“sumition”,“address”将更改为 使用正则表达式可以增强灵活性。例如,如果只希望在“add”是一行中的前三个字符的情况下进行更 以下是正则表达式遵循的基本规则:
像 grep 一样看世界。有许多工具与 grep 实用程序相似,如大多数其他操作系统中的 find 命令或一些编程语言中的 instr 为了解释其用法和结果,我们需要一个用来处理的核心文件。下框代表一个名为 MEMO
作为一个简单的示例,要查找包含单词“welcoming”的行,最好的方法是 $ grep welcoming MEMOPlease join me in welcoming each of our new team members. $ 如果要查找单词“market”,结果只略有不同: $ grep market MEMO In order to better serve the needs of the mass market customers, Alden Publishing is integrating This change will allow us to better coordinate our selling and marketing efforts, as well as $ grep 要查找所有包含数字的行,请使用以下方法: $ grep "[0-9]" MEMO73945 REARICK RALPH73947 KELLEY PHIL 73949 DUGAN GEORGE73950 WAGNER EVAN 73951 BOWL GORDON73952 PALMER SCOTT73954 MAY JAN 73955 HARRIS SHANNON 73956 BOYCE WILLIAM 73958 BRUMMER RICHARD 73960 WADDY RICHARD 73962 CHEESMAN BOB $ 要查找所有包含“the”的行,请使用以下方法: $ grep the MEMOIn order to better serve the needs of the mass market customers,Alden Publishing is integratingthe groups selling to this channel for Alden General Reference andAlden Computer Publishing. simplify Alden's relationships with these customers in the areasof customer service, and Bob Cheesman have joined the sales team as result of thesechanges.To achieve this goal, we have also organized the new mass salesgroup into three distinct teamsthe national account managers and their respective accounts ineach of the teams.We have also$ 找到了六行,其中一行实际上包含单词“these”,余下几行包含单词“the”的精确匹配。grep 实用程序(几乎与其他每个 $ grep The MEMOcoverage.They include:$ 如果不考虑文件中使用的大小写而要查找某个特定词组,则可以采用下面两种方法。第一种方法是使用方 $ grep "[T,t]he" MEMOIn order to better serve the needs of the mass market customers,Alden Publishing is integratingthe groups selling to this channel for Alden General Reference andAlden Computer Publishing. simplify Alden's relationships with these customers in the areasof customer service, and Bob Cheesman have joined the sales team as result of thesechanges.To achieve this goal, we have also organized the new mass salesgroup into three distinct teamsthe national account managers and their respective accounts ineach of the teams.We have alsocoverage.They include: $ 第二种方法是使用“-i”选项,该选项指示 grep 不区分大小写: $ grep -i the MEMOIn order to better serve the needs of the mass market customers,Alden Publishing is integratingthe groups selling to this channel for Alden General Reference andAlden Computer Publishing. simplify Alden's relationships with these customers in the areasof customer service, and Bob Cheesman have joined the sales team as result of thesechanges.To achieve this goal, we have also organized the new mass salesgroup into three distinct teamsthe national account managers and their respective accounts ineach of the teams.We have alsocoverage.They include: $ 除“-i”以外,您还可以使用几个其他选项来更改输出。最相关的选项包括:
使用 fgrep
例如,假设您要从前面的 MEMO $ grep HARRIS MEMO73955 HARRIS SHANNON $ 您可以只使用一个 fgrep 命令来完成同一操作: $ fgrep "HARRIS> BOB" MEMO73955 HARRIS SHANNON 73962 CHEESMAN BOB $ 注意,在各项之间需要有一个回车符。如果没有回车符,搜索将在每行上查找与“HARRIS 您不必在命令行上指定搜索项,而是可以将其置于文件中并使用该文件的内容搜索其他文件。使用 -f $ cat pull_listHARRISBOB$ 同其前辈 grep 和 fgrep 一样,egrep用于在文件中搜索指定文本的匹配项。从本质上而言,您可以将它看作是 grep $ egrep "HARRIS> BOB" MEMO73955 HARRIS SHANNON 73962 CHEESMAN BOB $ 除了能够搜索多个对象以外,egrep 提供的 grep 所不具备的功能还包括能够搜索重复项和组: 搜索前面字符的零个或一个重复项 例如,假设您不知道 Jan 的姓是“May”还是“Mays”: $ egrep "MAYS?"MEMO73954 MAY JAN $ 该方法查找“MAY”和“MAYS”的匹配项,而 $ egrep "HARRIS+" MEMO73955 HARRIS SHANNON $ 匹配“HARRIS”、“HARRISS”、“HARRISSS”,依此类推。如果要搜索某个单词 $ egrep -i "electron(ic)?s" MEMOJan May, who joins us from Acme Consumer Electronics as a National Account Manager$ 该方法查找“electrons”和“electronics”的匹配项。因此,后跟“+”的正则 实例。grep 实用程序系列可以用于任何文本格式的系统文件,在其行中查找匹配项。例如,要在 /etc/passwd $ grep kristin /etc/passwdkristin:petKv.fLWG/Ig:506:100:kristin dulaney:/home/kristin:/bin/bashkscott:#Jknidies^v:610:100:kristin scott:/home/kscott:/bin/bash$ 注意:本文的很多示例都使用了 由于它在行中的任意位置搜索匹配项,因此它将找到 Kristin Dulaney 和 $ grep "^kristin" /etc/passwd kristin:petKv.fLWG/Ig:506:100:kristindulaney:/home/kristin:/bin/bash$ 使用 cut、paste cut 实用程序能够将可能构成数据域的列与文件分离。默认分隔符为制表符,-f one two threefour five sixseven eight nineten eleven twelve 则命令 $ cut -f2 august 将返回 twofiveeighteleven 而 $ cut -f1,3 august 将返回相反的结果: one threefour sixseven nineten twelve 该命令有多个可用选项。除 -f 以外的两个比较常见的选项是 -c—用于指定字符而非域 其他 cut 选项。 ls -l $ ls -l | cut -d" " -f5 该命令忽略权限(第一个域)、两组空格(第二和第三个域)以及链接数(第四个域)。随后,它将显示 $ ls -l | cut -c16 返回第 16 $ ls -l | cut -c16-24 将返回名称域中的那些项。 文件名从第 55 $ ls -l | cut -c55- cut IndianapolisColumbusPeoriaLivingstonScottsdale 第二个文件的内容为 IndianaOhioIllinoisMontanaArizona 则以下内容(包括提示)将为生成的结果: $ paste fileone filetwoIndianapolis IndianaColumbus OhioPeoria IllinoisLivingston MontanaScottsdale Arizona$ 如果第一个文件中的行数比第二个文件多,则只粘贴制表符之后的空白项。制表符是默认分隔符,但可以 $ paste -d", " fileone filetwoIndianapolis, IndianaColumbus, OhioPeoria, IllinoisLivingston, MontanaScotttsdale, Arizona$ 也可以使用 -s 选项将第一个文件的所有数据输出到一行,其后依次是一个回车符和第二个文件: $ paste -s fileone filetwoIndianapolis Columbus Peoria Livingston Scotttsdale Indiana Ohio Illinois Montana Arizona$ 可以将 join 实用程序看作是 paste 的显著增强版本。但该实用程序只有在所联接的文件存在一个公共域时才起作用,这一点非常重要。例如,如果像前面 paste 示例中那样使用 join,则结果将如下所示: $ join fileone filetwo$ 换言之,不显示任何内容。join
第二个文件的内容包括
以下内容(包括提示)是生成的结果: $ join fileone filetwo11111 Indianapolis Indiana 500 race22222 Columbus Ohio Buckeye State33333 Peoria Illinois Wrigley Field44444 Livingston Montana Yellowstone Park55555 Scottsdale Arizona Grand Canyon$ 已识别第一个域相同,并合并了匹配项。paste
则以下内容(包括提示)是生成的结果: $ join fileone filetwo11111 Indianapolis Indiana 500 race22222 Columbus Ohio Buckeye State$ 只要文件不再匹配,就不再执行操作。检查而且仅检查两个文件中的对应行,在默认域上查找匹配。如果 $ tac filetwo > filethree$ join fileone filethree55555 Scottsdale Arizona Grand Canyon$ 尽管这两个文件中的每一行都匹配,但只找到一个匹配项。强烈建议您首先对每个要使用的文件进行排序 您不必保留 join 的默认设置,即只查看前几个域是否匹配,或输出所有列。-1 选项使您能够指定将哪个域用作第一个文件中的匹配域,-2 $ join -1 2 -2 3 fileone filethree -o 选项用于以格式 $ join -o 1.2 2.3 fileone filethreeIndianapolis 500Columbus BuckeyePeoria WrigleyLivingston YellowstoneScottsdale Grand$ 实例。由于 cut $ cut -d":"-f1 /etc/passwdrootdaemonbinsysadmuucpmailkristin$ 要收集用户名及其相应的主目录,可以提取第一个和第六个域: $ cut -d":"-f1 /etc/passwdroot:/daemon:/bin:/usr/binsys:/adm:/var/admuucp:/usr/lib/uucpmail:/etc/mailkristin:/home/kristin$ join $ join -1 4 -2 3 -o 1.1 2.1 1.6 -t :/etc/passwd /etc/group 千万别忽视 sed
Linux 工具中两个最强大的实用程序是 sed 和 awk。sed 流编辑器可用于编辑大量数据,而不需要任何的手动干预。awk 实用程序本身实际上是一种编程语言,可用于复杂的逻辑语句以及简化文本片段的提取。 这两个实用程序是先前文章的主题;建议您在进行下一个示例前先 实例。 要从 $ awk -F:'{print $6}' /etc/passwd 要打印该文件(使用短划线代替域之间的分隔符)并只打印第一个和第六个域(按相反的顺序),请使用 $ awk -F":"'{OFS="-"}{print $6,$1}}' /etc/passwd 结论 本文重点介绍了用于从标准 Linux |
|
来自: dwlinux_gs > 《Linux shell脚本》