LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   BASH: Output everything between two strings (https://www.linuxquestions.org/questions/programming-9/bash-output-everything-between-two-strings-266014/)

systemparadox 12-13-2004 03:28 PM

BASH: Output everything between two strings
 
Hi. im sure this sort of question is probably asked a lot, but I can't seem to find any helpful info.
I am writing a bash script. What I want is something which will search a file and output everything between two strings. e.g. if the file contains something like this:
<HTML>
<!--Description="this is a random example page"-->
<BODY>
<!--Heres some content-->
blahblahblahdablah
</BODY>
</HTML>

i want to put in something like: from "<!--Description=\"" to "\"-->" and get:
this is a random example page

I did try using grep, but I havent the faintest idea how to use it, and the man page didnt make it look like it was capable of this.

Thanks in advance
Simon

Tinkster 12-13-2004 05:43 PM

Just a first shot ...
Code:

grep "<!--" temp.html | sed 's/<!--\(.*\)-->/\1/g'

Gives


Cheers,
Tink
Code:

grep '<!--' temp.html | sed 's/<!--\(.*\)-->/\1/g'
Description="this is a random example page"
Heres some content

with your snippet as temp.html

systemparadox 12-18-2004 10:26 AM

Thanks!
I didn't actually want it to output the "Here's some content" bit, I put that in there because I wanted it to destinguish between <!--Description and other comments. Anyways that doesn't matter because I managed to modify your example to do that.

It now looks like this:

Code:

grep '<!--Description' temp.html | sed 's/<!--Description="\(.*\)"-->/\1/g'
and gives:

Code:

this is a random example page
Thanks for the help!
Simon


All times are GMT -5. The time now is 02:35 PM.