awk -v var=$var /var/ 没有按预期工作

内幕

文件部分:

 <style:style style:name="P15" style:family="paragraph" style:parent-style-name="Table_20_Contents">
      <style:paragraph-properties fo:text-align="center" style:justify-single-word="false"/>
      <style:text-properties style:font-name="open sansregular2" fo:font-size="18pt" fo:font-weight="normal" officeooo:rsid="00300000" officeooo:paragraph-rsid="00100000" style:font-size-asian="18pt" style:font-weight-asian="normal" style:font-size-complex="18pt" style:font-weight-complex="normal"/>
    </style:style>
    <style:style style:name="P16" style:family="paragraph" style:parent-style-name="Table_20_Contents">
      <style:paragraph-properties fo:text-align="center" style:justify-single-word="false"/>
      <style:text-properties fo:color="#000000" style:font-name="open sansregular2" fo:font-size="18pt" officeooo:rsid="00050000" officeooo:paragraph-rsid="000040000" style:font-size-asian="18pt" style:font-size-complex="18pt"/>
    </style:style>
    <style:style style:name="P17" style:family="paragraph" style:parent-style-name="Table_20_Contents">
      <style:paragraph-properties fo:text-align="center" style:justify-single-word="false"/>
      <style:text-properties fo:color="#000000" style:font-name="open sansregular" fo:font-size="18pt" officeooo:rsid="00100002" officeooo:paragraph-rsid="00100002" style:font-size-asian="18pt" style:font-size-complex="18pt"/>
    </style:style>
awk '/\<style:style style:name="P16"/,/style:style\>/' RS='\</style:style\>' file

虽然可能没有正确形成,但会产生预期的结果:

<style:style style:name="P16" style:family="paragraph" style:parent-style-name="Table_20_Contents">
      <style:paragraph-properties fo:text-align="center" style:justify-single-word="false"/>
      <style:text-properties fo:color="#000000" style:font-name="open sansregular2" fo:font-size="18pt" officeooo:rsid="00050000" officeooo:paragraph-rsid="000040000" style:font-size-asian="18pt" style:font-size-complex="18pt"/>

(我想要整个块,包括</style:style>,但可以按原样使用),但是,

echo $TPNum
"P16"
awk -v TPNum=$TPNum '/\<style:style style:name=TPNum/,/style:style\>/' RS='\</style:style\>' file

除了相同的警告外,不会产生任何结果:awk: warning: escape sequence `\<' treated as plain `<' awk: warning: escape sequence `\>' treated as plain `>'我之前在 awk 中使用过变量,没有任何问题。请问我在这里遗漏了什么?

钢刀

如果您的文件是 HTML 或 XML,那么您应该考虑使用专为标记语言设计的工具。

但是,如果您必须使用awk,那么 AFAIK 您不能在正则表达式常量中 使用变量/.../但是,您可以使用 GNU awk 用户指南所指的动态正则表达式或计算正则表达式- 基本上是您可以在~比较的 RHS 上使用的字符串表达式所以:

$ TPNum='"P16"'

$ awk -v TPNum="$TPNum" '
    $0 ~ "\\<style:style style:name="TPNum{p=1} p{print} /style:style>/{p=0}
  ' file
    <style:style style:name="P16" style:family="paragraph" style:parent-style-name="Table_20_Contents">
      <style:paragraph-properties fo:text-align="center" style:justify-single-word="false"/>
      <style:text-properties fo:color="#000000" style:font-name="open sansregular2" fo:font-size="18pt" officeooo:rsid="00050000" officeooo:paragraph-rsid="000040000" style:font-size-asian="18pt" style:font-size-complex="18pt"/>
    </style:style>

反斜杠需要在动态正则表达式中转义,因为字符串被扫描了两次:

如果字符串被扫描两次有什么区别?答案与转义序列有关,尤其是与反斜杠有关。要将反斜杠转换为字符串内的正则表达式,您必须键入两个反斜杠。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章