Solved

How to use grep to search blocks of lines ???

Posted on 2004-10-19
5
214 Views
Last Modified: 2013-12-26
Hi,

I want to build a script that can grab certain paramethers out of the xml-like tags. It is hard to explain without an example so, let me start with an example.

Let's say I have a file called xyz.html that contains:

<html>
  <head>
    <title>xyz</title>
  <head>
  <body>
    <img src="\home\pics\1.gif">
    <!-- <img src="\home\pics\2.gif">
           <img src="\home\pics\3.gif">
         <img src="\home\pics\4.gif">
         <img src="\home\pics\5.gif"> -->
    <img src="6.gif">
    <!-- <img src="\home\pics\7.gif"> -->
  </body>
</html>

Now I want to extract all the images that are within the comment tags (<!-- -->), in this case they'll be 2.gif, 3.gif, 4.gif, 5.gif and 7.gif. By using grep expression (such as : grep "<!--(.*)-->" *.*), I can easily extract 7.gif, but extracting images out of the commented block is a challenge. So is there a way to do this with grep or any other utilities?

Thanks,
neomodus.
0
Comment
Question by:neomodus
  • 4
5 Comments
 
LVL 11

Expert Comment

by:cjjclifford
ID: 12347396
the following will print all lines that are contain comment lines.

awk '/<!--/{inc=1}{if(inc==1)print}/-->/{inc=0}' filename
0
 
LVL 11

Accepted Solution

by:
cjjclifford earned 150 total points
ID: 12347420
then used sed to extract out the image name...

sed 's/img src=\"\(.*\)">/\1/g'

so all together:

awk '/<!--/{inc=1}{if(inc==1)print}/-->/{inc=0}' filename | sed 's/^.*img src=\"\(.*\)">.*$/\1/g'
0
 
LVL 2

Expert Comment

by:rajeshp_69
ID: 12397886
Here is a one line command to do the same

awk '/\!--/,/--\>/ {print}' xyz.html

I'll explain what this command does:
this will print all the lines between !-- and --> which is what we want.
0
 
LVL 11

Expert Comment

by:cjjclifford
ID: 12410102
that's only equivalent to my first post, my second post goes further to extract the "img" files - which is the real goal I believe.... (quote: "I want to extract all the images that are within the comment tags")
0
 
LVL 11

Expert Comment

by:cjjclifford
ID: 12410988
How does the accepted answer not solve the problem in the question? i.e. why do you think it deserves a "C" grade?
(quoted from http://www.experts-exchange.com/help.jsp#hi55) :

Grading at Experts Exchange is not like school. It's more like the "10-point Must" system in professional boxing; in other words, an answer is worth an A, unless it doesn't resolve your issue. If it requires you to do a little more research, or figure out one more piece of code, then it's worth a B. If you think it's not worth a B, the custom is to offer the Experts an opportunity to earn a better grade.


You should also remember that when you give a C, it shows up as part of your grading record. If you show a pattern of giving low grades, many Experts will simply avoid your questions.

Quoted from http://www.experts-exchange.com/help.jsp#hi73:

Although we use an A-D scale here at Experts Exchange, it works differently than, say, school grades. If one or more Experts' proposals are accepted as answers, they should usually be given an A or B grade, since they have taken the time to provide you with a working solution. If a possible solution is incomplete - ask for clarification or details before accepting the answer and grading it. People should not be given lower grades because of incorrect grammar or because you just accepted their answer or comment to close the question. Keep in mind, your question and any follow-up comments should be focused so that there can be a specific answer. The following is a good guideline to follow when grading:


A: The Expert(s) either provided you with a thorough answer or they provided you with a link to information that thoroughly answered your question. An "A" can also be given to any answer that you found informative or enlightening beyond the direct question that you asked.

B: The Expert(s) provided an acceptable solution, or a link to an acceptable solution, that you were able to use, although you may have needed a bit more information to complete the task.

C: Because Experts' reliability are often judged by their grading records, many Experts would like the opportunity to clarify if you have questions about their solutions. If you have given the Expert(s) ample time to respond to your clarification posts and you have responded to each of their posts providing requested information; or if the answers, after clarification, lack finality or do not completely address the issue presented, then a "C" grade is an option. You also have the option here of just asking Community Support to delete the question.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
function returning CString in a regular win32 DLL 8 150
dog bark java program 15 82
wordcount challenge 11 78
Not needed 13 58
In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

746 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now