LINUX.ORG.RU

поиск и замена нескольких строк в html-файлах


1

2

Есть ~200 html файлов. В них очень часто встречается определенная последовательность строк (10 штук, есть пустые). Нужно их заменить на них же + 10 новых строк, которые должны быть вставлены перед pattern`ом

Буду благодарен за хорошую ссылку или прототип кода

★★★★★

Последнее исправление: ZuBB (всего исправлений: 1)

Ответ на: комментарий от prischeyadro

нет, тестер.

И да, откуда уверенность? что б сп"№;«ть?

ZuBB ★★★★★
() автор топика
Ответ на: комментарий от wfrr

не совсем понял но вот сам паттерн

			<tr class="comment">
				<!-- Storing ID of the HTML element that holds Javascript window -->
				<td colspan="3">Storing ID of the HTML element that holds Javascript window</td>
			</tr>

			<tr>
				<td>storeActiveJSWinID</td>
				<td>winID</td>
				<td></td>
			</tr>

ZuBB ★★★★★
() автор топика
Ответ на: комментарий от ZuBB

замена



			<tr class="comment">
				<!-- Verifying that there is no any present Javascript windows -->
				<td colspan="3">Verifying that there is no any present Javascript windows</td>
			</tr>

			<tr>
				<td>waitForElementPresent</td>
				<td>css=body > div.zpWinThemeFunambol:last-child</td>
				<td></td>
			</tr>

			<tr class="comment">
				<!-- Storing ID of the HTML element that holds Javascript window -->
				<td colspan="3">Storing ID of the HTML element that holds Javascript window</td>
			</tr>

			<tr>
				<td>storeActiveJSWinID</td>
				<td>winID</td>
				<td></td>
			</tr>

ZuBB ★★★★★
() автор топика

Python+BeautifulSoup должны помочь.

fat_angel ★★★★★
()

perl :) хоть и банально

Jetty ★★★★★
()
Ответ на: комментарий от ZuBB

[code]#!/bin/sed -rf \~\s*<tr class=«comment»>~N \~\s*<!-- Storing ID of the HTML element that holds Javascript window -->~N \~\s*<td colspan=«3»>Storing ID of the HTML element that holds Javascript window</td>~N \~\s*</tr>~N \~\s*~N \~\s*<tr>~N \~\s*<td>storeActiveJSWinID</td>~N \~\s*<td>winID</td>~N \~\s*<td></td>~N \~\s*</tr>~{    /^[^\n]*(\n[^\n]*){9}/{       s/^/FOUND\nLINES\n/       b    } } [/code] вроде работает. только добавка к началу паттерну из 2х строк - писать лень.

drBatty ★★
()
Ответ на: комментарий от drBatty
#!/bin/sed -rf
\~\s*<tr class="comment">~N
\~\s*<!-- Storing ID of the HTML element that holds Javascript window -->~N
\~\s*<td colspan="3">Storing ID of the HTML element that holds Javascript window</td>~N
\~\s*</tr>~N
\~\s*~N
\~\s*<tr>~N
\~\s*<td>storeActiveJSWinID</td>~N
\~\s*<td>winID</td>~N
\~\s*<td></td>~N
\~\s*</tr>~{
	/^[^\n]*(\n[^\n]*){9}/{
		s/^/FOUND\nLINES\n/
		b
	}
}
drBatty ★★
()

Не слушай никого из советующих использовать регулярные выражения для разбора контекстно-свободных грамматик. Задача проста и примитивна. Если использовать правильные инструменты, конечно же. С помощью парсера преобразуешь html в дерево. Затем обходишь дерево, сравнивая узлы с нужной последовательностью, как найдёшь, делай

(setf узел что-нада)
После чего, используя генератор сохраняешь изменённый файл на диск. Всё.

ugoday ★★★★★
()
Ответ на: комментарий от ugoday

Специфика html, к сожалению, такова, что в дерево оно преобразуется не всегда.

Reset ★★★★★
()
Ответ на: комментарий от note173

Можно, конечно, настроить парсер, чтобы он чинил битый html, но лучше предать огню его автора.

ugoday ★★★★★
()
Ответ на: комментарий от ZuBB

> замена

#!/usr/bin/perl

use strict;
use File::Slurp;

my $fragment = <<EOF;
         <tr class="comment">
            <!-- Verifying that there is no any present Javascript windows -->
            <td colspan="3">Verifying that there is no any present Javascript windows</td>
         </tr>

         <tr>
            <td>waitForElementPresent</td>
            <td>css=body > div.zpWinThemeFunambol:last-child</td>
            <td></td>
         </tr>
EOF

for my $file (@ARGV) {
    $_ = read_file $file or die "$file: $!\n";
    write_file $file, $_ if s;^(?=\s*<tr class="comment">\s+<!-- Storing ID);$fragment;gm;
}

типа так?

arsi ★★★★★
()
Вы не можете добавлять комментарии в эту тему. Тема перемещена в архив.