Solved

Range printing with sed.

Posted on 2007-04-04
7
1,247 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 45

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
The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

 
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 50 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

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

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

I have been reconstructing a PHP-based application that has grown into a full blown interface system over the last ten years by a developer that has now gone into business for himself building websites. I am not incredibly fond of writing PHP code o…
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 several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:

717 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