Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
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
up vote 0 down vote accepted

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

share|improve this answer

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.

share|improve this answer
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
@manatwork thanks for pointing that out. – peterph Dec 27 '12 at 16:25

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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