LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-13-2004, 03:28 PM   #1
systemparadox
Member
 
Registered: Jul 2004
Location: Tavistock, Devon, England
Distribution: Slackware 9.1, Mandrake 9.0/9.1/10.0, LFS 6.0, Knoppix 3.1/3.3
Posts: 73

Rep: Reputation: 15
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
 
Old 12-13-2004, 05:43 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
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
 
Old 12-18-2004, 10:26 AM   #3
systemparadox
Member
 
Registered: Jul 2004
Location: Tavistock, Devon, England
Distribution: Slackware 9.1, Mandrake 9.0/9.1/10.0, LFS 6.0, Knoppix 3.1/3.3
Posts: 73

Original Poster
Rep: Reputation: 15
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
 
  


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
bash script help (arrays and strings from files) nkoplm Programming 14 12-02-2005 09:50 AM
how to find duplicate strings in vertical column of strings markhod Programming 7 11-02-2005 04:04 AM
Bash scripting: column-ize file of varying length strings Quantum0726 Programming 4 08-13-2005 06:19 PM
bash and strings graziano1968 Linux - Software 2 10-01-2004 06:50 AM
adding strings in bash FireAge Linux - General 4 03-11-2003 10:57 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:22 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