我有一个大的html文件,带有像这样的img标签
<p class="Equation_left_column ParaOverride-2"> <img class="_idGenObjectAttribute-1" src="image/Image353.png" alt=""> </p>
<p class="Equation_left_column ParaOverride-2"> <img class="_idGenObjectAttribute-2" src="image/Image376.png" alt=""> </p>
<p class="Equation_left_column ParaOverride-2"> <img class="_idGenObjectAttribute-3" src="image/Image385.png" alt=""> </p>
我想替换每个img标签
<img class="_idGenObjectAttribute-." src="image/Image...png" alt="" />
带有位于外部文件中的文本
out1.txt
out2.txt
out3.txt
所需的输出
<p class="Equation_left_column ParaOverride-2">out1.txt (more precisely the text of this file) </p>
<p class="Equation_left_column ParaOverride-2">out2.txt (more precisely the text of this file)</p>
<p class="Equation_left_column ParaOverride-2">out3.txt (more precisely the text of this file)</p>
是否有办法,在awk / sed / etc中执行命令或程序来实现此目的?
答案 0 :(得分:2)
试试这个
awk '
FILENAME ~ /Out[0-9]+/ {if(File!=FILENAME)i++;Out[i]=Out[i] $0;File=FILENAME;next}
/{img class="_idGenObjectAttribute-[0-9]+" src="/{sub(/{img class="_idGenObjectAttribute-[0-9]+" src="[^"]*" alt=""}/, Out[++fi])}
1' Out*.txt YourFile
<强>解释强>
FILENAME ~ /Out[0-9]+/
选择属于任何Out *文件的行(默认情况下,awk逐行工作)
if(File!=FILENAME)i++
是每个新文件名递增的索引计数器Out[i]=Out[i] $0
将保留在内存(数组)Out文件的每一行(与之前的索引关联)File=FILENAME;next
提醒上一个文件处理(下一次迭代)并循环到下一行进行处理 /{img class="_idGenObjectAttribute-[0-9]+" src="/
(并且文件是由于前一条指令而导致的最后一行)
sub(/.../, Out[++fi])
用数组条目号fi
的内容替换模式。在调用数组之前,此索引会递增。内容对应于第一系列指令中的加载文件 Out*.txt YourFile
将所有 Out 文件作为输入,并完成您要处理的文件