Quote:
Originally Posted by atagore
When searching for "text" in a file,
I would like to see the line containing "text"
and the line preceeding
and the line after
So I would like a 3 line output for each line matched by grep
Is there a quick way to accomplish this?
Example--
FILE.DAT contains
abcdef
123456789
xyz
hijklmnop
987654321
I would like the search for "xyz" to yield results such as:
FILE.DAT:2: 123456789
FILE.DAT:3: xyz
FILE.DAT:4: hijklmnop
Any suggestions?
A Tagore
atagore2@juno.com
Hey as per my knowledge you can try the cgrep command. In this command 1,1 represent matching line before and after. At the bottom of this reply i have added the usage of this command.
Command : cgrep -1,1 "xyz" FILE.DAT
Output :
123456789
xyz
hijklmnop
Usage of cgrep command :
Usage: cgrep [-pre[,post]] [-p] [-v] [-c] [-h] [-n] [-d string]
[-e] pattern [file...]
cgrep is a context grep. It displays more than the one matching line for
every match (2 before and 2 after as default).
-3 means display 3 lines before and 3 lines after the match (e.g.)
-5,12 means display 5 lines before the match and 12 lines after (e.g.)
-p means display only as much of the context as belongs to the
current paragraph. (paragraphs bounded by empty lines)
-v means invert search (display nomatches)
-c means display number of matching lines at the end of run
-h means toggle display filename before every line
-i means ignore case when matching the regexp
-n means display line number before every line
-d string means use string as the output delimiter string
-- (or -e) means end options (i.e. now comes the pattern,
for patterns starting with - )
pattern is a Perl regular expression (you better quote it !)
Please let me know if this works fine.