[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now


Range printing with sed.

Posted on 2007-04-04
Medium Priority
Last Modified: 2008-01-09
I'm trying, in vain I'm afraid, to teach myself some sed.  I'd like to print a range of a data file.  My output sections in the data file are delimited (Using the 'SYBASE' section as an example:
Line to print
Line to print
Line to print
I've tried:
sed -n "s/\<SYBASE\>/,/\<\/SYBASE\>/p" notesfile

Any comments would be appriciated.

Question by:Just_RC
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
LVL 58

Expert Comment

ID: 18853351
An extra s must be the error ...

sed -n "/\<SYBASE\>/,/\<\/SYBASE\>/p" notesfile

This should also work...

sed -n "/<SYBASE>/,/<\/SYBASE>/p" notesfile
LVL 46

Expert Comment

by:Kent Olsen
ID: 18853373
Hello Just_RC,

Close.  Very close.  You want to search for the string, not substitute text within it.

sec -n '/\<SYBASE\>/,/\<\/SYBASE\>/p' notesfile

Good Luck,

Author Comment

ID: 18853389
Amit g:
Closer....it dumps the entirety of the 'notesfile'.  My intention was just to print the lines between the range definitions...in your understanding, isn't that what the '-n' switch does?

Thank you for your time and attention.

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 58

Expert Comment

ID: 18853410
It should only print the lines between




inclusive. Anything outside shouldn't be printed.

Author Comment

ID: 18853737
I've doublechecked my syntax and I'm still dumping the entire file out to STDOUT.

None the less, thank you.
LVL 58

Accepted Solution

amit_g earned 200 total points
ID: 18853773
What *nix you have?

Could you try these command individually and let us know what you get from each one of them?

sed -n '' notesfile
sed -n 'p' notesfile
sed -n '1,5p' notesfile
sed -n '/<SYBASE>/p' notesfile
sed -n '/<SYBASE>/,/<\/SYBASE>/p' notesfile

Author Comment

ID: 18853881
OS/Hardware: Solaris 5.6 on SPARCstation-5
sed -n "notesfile -->Unrecognized Command
sed -n 'p' notesfile --> Dumps entire file
sed -n '1,5p' notesfile --> Dumps first 5 lines of file (Same as head -5 notesfile)
sed -n '/<SYBASE>/p' notesfile --> Dumps '<SYBASE>'
sed -n'/<SYBASE>/,/<\/SYBASE>>/p' notesfile --> Whoohoo...

Must have been me escaping the '>'?

Wow...too cool Thank you.

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When you do backups in the Solaris Operating System, the file system must be inactive. Otherwise, the output may be inconsistent. A file system is inactive when it's unmounted or it's write-locked by the operating system. Although the fssnap utility…
As most anyone who uses or has come across them can attest to, regular expressions (regex) are a complicated bit of magic. Packed so succinctly within their cryptic syntax lies a great deal of power. It's not the "take over the world" kind of power,…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Suggested Courses

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question