Solved

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

Posted on 2004-10-19
5
226 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
[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
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

691 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