bash - 如何使用 sed(或 awk)进行链接规范化,获取文件名?

标签 bash sed awk

我有一个 200 页的网站,想要实现链接规范化。

我使用我的 ftp 客户端将站点下载到本地目录中,并希望在 <head> 下有规范的元标记。每个页面的标记。

所以,对于第 1 页,我想转换

<head>

进入

<head>
<link rel="canonical" href="http://www.site.com/page1.htm" />

并使用 sed 在整个本地目录(page1.htm、page2.htm ... page200.htm)中执行此操作。谢谢。

最佳答案

sed , awk不是为处理 HTML 而设计的。参见 RegEx match open tags except XHTML self-contained tags

演示使用 , ,

cd /where/HTML_pages/exists
for file in *html; do xmlstarlet transform --html <(cat<<EOF
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" >
    <xsl:output method="html" encoding="utf-8"/>
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()" />
        </xsl:copy>
    </xsl:template>
     <xsl:template match="head">
         <xsl:copy>
             <xsl:apply-templates/>
             <xsl:if test="not(link)">
                 <link rel="canonical" href="http://www.site.com/$file" />
             </xsl:if>
         </xsl:copy>
     </xsl:template>
 </xsl:stylesheet>
EOF) >/"tmp/$file" "$file" && mv "/tmp/$file" "$file"
done

编辑

一个更好/合适的纯解决方案仍在使用 但是现在不再是强制性的:

文件 xsl.xslt :

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
   <xsl:output method="html" encoding="utf-8" />
   <!-- where are not making a HTML from scratch,
         so we will copy what's exists -->
   <xsl:template match="@*|node()">
      <xsl:copy>
         <xsl:apply-templates select="@*|node()" />
      </xsl:copy>
   </xsl:template>
   <!-- looking for "head" tag -->
   <xsl:template match="head">
      <xsl:copy>
         <xsl:apply-templates />
         <!-- if "link" tag not exists ... -->
         <xsl:if test="not(link)">
            <!-- we add the new "link" tag... -->
            <link>
               <xsl:attribute name="rel">
                  <!-- with a fixed string attribute... -->
                  <xsl:text>canonical</xsl:text>
               </xsl:attribute>
               <xsl:attribute name="href">
                  <!-- and a dynamic string attribute ("link" parameter) -->
                  <xsl:value-of select="$link" />
               </xsl:attribute>
            </link>
         </xsl:if>
      </xsl:copy>
   </xsl:template>
</xsl:stylesheet>

代码:

cd /where/HTML_pages/exists
for file in *html; do
    xmlstarlet transform \
        --html \
        xsl.xslt \
        -s "link=http://www.site.com/$file" "$file" > "/tmp/$file" &&
            mv "/tmp/$file" "$file"
done

这将在 <head> 中添加您想要的元素以当前页面为变量

关于bash - 如何使用 sed(或 awk)进行链接规范化,获取文件名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19796899/

相关文章:

linux - 如何在继续下一项之前检查 cp 是否已完成

bash - 使用 AWS4 身份验证通过 bash 将文件上传到 s3?

linux - 查找包含特定文本的行,并打印该文本之后的内容

linux - 如何使用 sed 复制十六进制格式的行?

indexing - awk - 打印包含初始分析中找到的最大值的所有行

awk - 如何在awk中按变量搜索

perl - 使用 sed、perl、awk、tr 或任何东西只删除文本文件中的单个空格

python - shell 脚本中的 virtualenvwrapper 函数不可用

bash - 如何在模式 "Using SED"之前插入字符串或换行符(不替换模式)在 MAC OS