If ruby
is available you can do the following
ruby -e 'puts readlines.join[/(?<=<tr>).+(?=<\/tr>)/m].gsub(/<\/?tr>/, "")' file
where file
is your input html file. The command executes a Ruby one-liner. First, it reads all lines from file
and joins them to a string, readlines.join
. Then, from the string it selects anything between (but not including) <tr>
and <\/tr>
that is one character or longer irrespective of newlines, [/(?<=<tr>).+(?=<\/tr>)/m]
. Then, it removes any <tr>
or </tr>
from the string, gsub(/<\/?tr>/, "")
(this is necessary to handle nested tr
tags). Finally, it prints the string, puts
.
You said that a html parser is not warranted for you but it is very easy to use Nokogiri with ruby
and it makes the command simpler.
ruby -rnokogiri -e 'puts Nokogiri::HTML(readlines.join).xpath("//tr").map { |e| e.content }' file
-rnokogiri
loads Nokogiri. Nokogiri::HTML(readlines.join)
reads all lines of file
. xpath("//tr")
picks out every tr
element and map { |e| e.content }
picks out the content for each element, i.e. what is between <tr>
and </tr>
.
'/<tr/{p=1}; p; /<\/tr>/{p=0}'
. Post some example input and expected output if it doesn't work.awk
is working but giving duplicates try to pass your awk's output fromsort -u
to get them distinct