LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Other *NIX Forums > Solaris / OpenSolaris
User Name
Password
Solaris / OpenSolaris This forum is for the discussion of Solaris, OpenSolaris, OpenIndiana, and illumos.
General Sun, SunOS and Sparc related questions also go here. Any Solaris fork or distribution is welcome.

Notices


Reply
  Search this Thread
Old 12-21-2007, 09:44 AM   #1
qipman
LQ Newbie
 
Registered: Jul 2006
Posts: 14

Rep: Reputation: 0
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
 
Old 12-21-2007, 09:56 AM   #2
marozsas
Senior Member
 
Registered: Dec 2005
Location: Campinas/SP - Brazil
Distribution: SuSE, RHEL, Fedora, Ubuntu
Posts: 1,499
Blog Entries: 2

Rep: Reputation: 68
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.
 
Old 12-21-2007, 12:59 PM   #3
qipman
LQ Newbie
 
Registered: Jul 2006
Posts: 14

Original Poster
Rep: Reputation: 0
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…
 
Old 12-21-2007, 02:04 PM   #4
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
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.
 
Old 12-26-2007, 04:29 AM   #5
marozsas
Senior Member
 
Registered: Dec 2005
Location: Campinas/SP - Brazil
Distribution: SuSE, RHEL, Fedora, Ubuntu
Posts: 1,499
Blog Entries: 2

Rep: Reputation: 68
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
SED and MV question wendtbd Red Hat 4 08-23-2006 11:22 AM
[sed] "Advanced" sed question(s) G00fy Programming 2 03-20-2006 12:34 AM
sed question lazyuser Programming 4 02-11-2005 06:11 AM
sed question sphynx Programming 6 04-28-2004 06:25 PM
little sed question freelinuxcpp Linux - Software 3 01-20-2004 07:36 AM

LinuxQuestions.org > Forums > Other *NIX Forums > Solaris / OpenSolaris

All times are GMT -5. The time now is 10:47 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration