?
Solved

Range printing with sed.

Posted on 2007-04-04
7
Medium Priority
?
1,255 Views
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:
<SYBASE>
Line to print
Line to print
Line to print
</SYBASE>
I've tried:
sed -n "s/\<SYBASE\>/,/\<\/SYBASE\>/p" notesfile

Any comments would be appriciated.

0
Comment
Question by:Just_RC
[X]
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
7 Comments
 
LVL 58

Expert Comment

by:amit_g
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
0
 
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,
Kent
0
 
LVL 2

Author Comment

by:Just_RC
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.

RC
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 58

Expert Comment

by:amit_g
ID: 18853410
It should only print the lines between

<SYBASE>

and

</SYBASE>

inclusive. Anything outside shouldn't be printed.
0
 
LVL 2

Author Comment

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

None the less, thank you.
0
 
LVL 58

Accepted Solution

by:
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
0
 
LVL 2

Author Comment

by:Just_RC
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.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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,…
Java performance on Solaris - Managing CPUs There are various resource controls in operating system which directly/indirectly influence the performance of application. one of the most important resource controls is "CPU".   In a multithreaded…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Suggested Courses

764 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