sreeja
sreeja

Reputation: 11

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

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

Upvotes: 0

Views: 173

Answers (2)

Josh Watzman
Josh Watzman

Reputation: 7260

Let's take it part by part.

  • INS= -- assigns the result of the command to the shell variable INS.
  • grep -- command to search for a string in a file.
  • grep "Insert completed" ${LOGFILE}.tmp -- searches for the string "Insert completed" in the file whatever-the-LOGFILE-variable-is.tmp.
  • | sed -- take the result of the grep command (i.e., the line in the file that has "Insert completed" in it) and send it to sed, the "stream editor".
  • s/ -- sed is an enormously powerful command and can do a million and a half things. This begins the "search and replace" feature of 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".
  • ^ -- 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"
  • . -- 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.
  • $ -- 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, i.e., the "any string" after "Insert completed" and before "row".
  • / -- 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.

Upvotes: 0

jaypal singh
jaypal singh

Reputation: 77105

grep command isn't doing anything special. It is just looking at your logfile specified via that variable and outputs all lines that contains the string Insert completed.

sed command however is doing what you think grep is doing. sed creates two capture groups (denoted by \(..\)). It grabs entire string from the beginning until Insert completed. and stores it in capture group 1. Second capture group stores rest of the string until row. Third capture group stores from row until end of line denoted by $. You replace this by capture group 2 (denoted by \2).

As a result the INS variable holds the output from sed's substitution.

Upvotes: 1

Related Questions