Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 240
  • Last Modified:

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

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
neomodus
Asked:
neomodus
  • 4
1 Solution
 
cjjcliffordCommented:
the following will print all lines that are contain comment lines.

awk '/<!--/{inc=1}{if(inc==1)print}/-->/{inc=0}' filename
0
 
cjjcliffordCommented:
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
 
rajeshp_69Commented:
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
 
cjjcliffordCommented:
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
 
cjjcliffordCommented:
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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now