简体   繁体   English

Unix shell脚本(grep命令)

[英]unix shell script(grep command)

i have to use the following 'grep' command to extract from a LOGFILE.Its working but could anyone please let me know how this below 'grep' command is working.?Please explain in detail.Thanks in advance.Please reply as i need to understand it 我必须使用以下'grep'命令从LOGFILE中提取。它可以正常工作,但是有人可以让我知道下面'grep'命令的工作方式吗?请详细解释。了解它

INS=`grep "Insert completed" ${LOGFILE}.tmp | sed 's/\(^.*Insert completed. \)\(.*\)\   
(row.*$\)/\2/'`

grep command isn't doing anything special. grep命令没有做任何特别的事情。 It is just looking at your logfile specified via that variable and outputs all lines that contains the string Insert completed . 它只是查看通过该变量指定的日志文件,并输出包含字符串Insert completed所有行。

sed command however is doing what you think grep is doing. sed命令正在执行您认为grep正在执行的操作。 sed creates two capture groups (denoted by \\(..\\) ). sed创建两个捕获组(用\\(..\\) )。 It grabs entire string from the beginning until Insert completed. 从开始到Insert completed.它将抓取整个字符串Insert completed. and stores it in capture group 1. Second capture group stores rest of the string until row. 并将其存储在捕获组1中。第二个捕获组将其余字符串存储到行中。 Third capture group stores from row until end of line denoted by $ . 第三个捕获组从rowrow末存储$ You replace this by capture group 2 (denoted by \\2 ). 您将其替换为捕获组2(用\\2表示)。

As a result the INS variable holds the output from sed's substitution. 结果, INS变量保存了sed's替换的输出。

Let's take it part by part. 让我们分一部分。

  • INS= -- assigns the result of the command to the shell variable INS . INS= -将命令的结果分配给外壳变量INS
  • grep -- command to search for a string in a file. grep用于在文件中搜索字符串的命令。
  • grep "Insert completed" ${LOGFILE}.tmp -- searches for the string "Insert completed" in the file whatever-the-LOGFILE-variable-is.tmp . grep "Insert completed" ${LOGFILE}.tmp -在文件whatever-the-LOGFILE-variable-is.tmp搜索字符串“插入完成”。
  • | sed | sed -- take the result of the grep command (ie, the line in the file that has "Insert completed" in it) and send it to sed , the "stream editor". | sed -获取grep命令的结果(即文件中“插入完成”的行)并将其发送到sed ,即“流编辑器”。
  • s/ -- sed is an enormously powerful command and can do a million and a half things. s/ sed是一个功能非常强大的命令,可以完成一百万个半任务。 This begins the "search and replace" feature of sed . 这将开始sed的“搜索和替换”功能。 Unlike the search and replace of a word processor, this is much more powerful, using "regular expressions" to find what to search and replace. 与文字处理器的搜索和替换不同,它更强大,使用“正则表达式”查找要搜索和替换的内容。 Basically, we define a "template" or "prototype" or similar of what sort of text we're looking for, instead of just an exact substring. 基本上,我们定义一个“模板”或“原型”或与我们要查找的文本相似的样式,而不只是一个确切的子字符串。
  • \\( -- begins a "capture group" in the sed regular expression. You can read up on the details of this elsewhere, but for the purposes of the command you posted, this just says "take whatever matches the subpattern until the next \\) and assign it a number I can use later". \\( -在sed正则表达式中开始一个“捕获组”。您可以在其他地方阅读此详细信息,但是出于您发布的命令的目的,这只是说“将匹配子模式的所有内容取到下一个\\)并为其分配一个编号,以后可以使用”。
  • ^ -- matches the beginning of the line. ^ -匹配行首。
  • .* -- matches a string of any length, but such that the rest of the line matches the other constraints in the regular expression. .* -匹配任意长度的字符串,但该行的其余部分匹配正则表达式中的其他约束。
  • Insert completed -- matches the exact string "Insert completed" Insert completed -匹配确切的字符串“插入完成”
  • . -- matches any one single character. -匹配任何一个字符。
  • \\) -- ends the capture group began above. \\) -结束捕获组从上面开始。
  • \\( -- starts a new capture group, the second one. \\( -启动一个新的捕获组,第二个。
  • .* -- matches a string of any length again. .* -再次匹配任意长度的字符串。
  • \\) -- ends the second capture group. \\) -结束第二个捕获组。
  • \\( -- starts the third capture group. \\( -启动第三个捕获组。
  • row.* -- matches the string "row" followed by anything. row.* -匹配字符串“ row”,后跟任何内容。
  • $ -- matches the end of a line. $ -匹配行尾。
  • \\) -- ends the third capture group. \\) -结束第三个捕获组。
  • / -- ends the "search" part of "search and replace". / -结束“搜索和替换”的“搜索”部分。 What follows is the "replace". 接下来是“替换”。
  • \\2 -- this refers to the contents of the second capture group above, ie, the "any string" after "Insert completed" and before "row". \\2这是指上面第二个捕获组的内容,即“插入完成”之后和“行”之前的“任何字符串”。
  • / -- ends the "replace" part of "search and replace". / -结束“搜索和替换”的“替换”部分。

So this looks for lines with "Insert completed" in your logfile, finds the part of them after "Insert completed" but before "row", and snips the line down to just that part. 因此,这会在日志文件中查找带有“插入完成”的行,在“插入完成”之后但在“行”之前找到其中的一部分,然后将行剪断到该部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM