I see that in my code I can modify a line if the entire code is based on that line. Example: <p class="example">this is an example Happy Holidays</p>. However if the </p> tag is located on a second line the code will pass that line.

What is the best way to detect until the line ends for certain tags?

  • 5
    1) Text manipulation tools are not suitable for parsing HTML. 2) Regular expressions are not suitable for parsing HTML. Based on 1) and 2): pick a dedicate HTML parser library and write a script which uses it. Otherwise you can play with setting the RS in awk to “>” or “</[^>]+>”.
    – manatwork
    Dec 27 '12 at 14:50

Simple answer is: do not use shell to parse XML. Use a XML parser instead, e.g. one of the numerous for Perl: XML::Parser, XML::Simple, or any other language. For HTML, HTML::Parser is an option (if we stay with Perl).

If you want to use bash, you can play with the read built-in and a loop a bit.

  • 1
    As this question is tagged html, better suggest HTML parsers instead. Very few HTML documents are syntactically valid and XML parsers are picky on syntax errors by their nature.
    – manatwork
    Dec 27 '12 at 15:04

Text utilities work on lines (text lines being (not too long) sequences of non-NUL characters terminated by a newline character). awk is the one standard utility that can be told to work on record separated by other things than newline characters, that's why awk talks of records instead of lines.

For instance, you could use > as the record separator. As in:

awk -v RS='>' ...

Another approach is to swap the character you want to use as the record separator (for other tools than awk) with the newline character:

... |
  tr '\n>' '>\n' |
  sed ... |
  other-text-utility... |
  tr '\n>' '>\n'

Those assume that the things you want to modify don't include nested HTML tags as they would start new records.

That is replace

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.