LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Solaris / OpenSolaris (https://www.linuxquestions.org/questions/solaris-opensolaris-20/)
-   -   SED question (https://www.linuxquestions.org/questions/solaris-opensolaris-20/sed-question-608357/)

qipman 12-21-2007 09:44 AM

SED question
 
Hello.

I have a SED question. If the following command prints out "patterntomatch" as well as the next 3 lines from a file, how can it be written to grep out the string and the 3 lines above it?

sed -ne "/patterntomatch/{p;n;p;n;p;}" filename

marozsas 12-21-2007 09:56 AM

Next time, try to read the related man pages, before post.
From the grep's man page:
Code:

          -B NUM, --before-context=NUM
              Print  NUM  lines  of  leading  context  before  matching lines.
              Places  a  line  containing  --  between  contiguous  groups  of
              matches.


qipman 12-21-2007 12:59 PM

Actually, I did read through the man pages, as I always do before posting.

I am new to the sed command and did see that in the man pages but did not interpret it this way. I am still not sure how to convert my command above to do what I need it to using the –B NUM argument…

jlliagre 12-21-2007 02:04 PM

Just a side comment. This "-B" option is a Gnu extension so isn't supported by Solaris grep nor the standard compliant /usr/xpg4/bin/grep.

It is available under the "ggrep" name under Solaris.

marozsas 12-26-2007 04:29 AM

Thanks jlliagre for pointing the right command in solaris.
Hi qipman, take a look on -A, -B and -C flags of ggrep. They will print lines n lines around the line it find a match (after, before and around). So you can use something like "ggrep -A 3 patterntomatch filename" to print the line that have a match and the next 3 lines.


All times are GMT -5. The time now is 05:24 AM.