AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / ubuntu / 问题 / 1307227
Accepted
Insideup
Insideup
Asked: 2021-01-12 18:49:18 +0800 CST2021-01-12 18:49:18 +0800 CST 2021-01-12 18:49:18 +0800 CST

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

  • 772

文件部分:

 <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 中使用过变量,没有任何问题。请问我在这里缺少什么?

command-line
  • 1 1 个回答
  • 85 Views

1 个回答

  • Voted
  1. Best Answer
    steeldriver
    2021-01-12T19:24:53+08:002021-01-12T19:24:53+08:00

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

    但是,如果您必须使用awk,那么 AFAIK 您不能在regexp 常量 /.../中使用变量。但是,您可以使用 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>
    

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

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

    • 1

相关问题

  • 如何从命令行仅安装安全更新?关于如何管理更新的一些提示

  • 如何从命令行刻录双层 dvd iso

  • 如何从命令行判断机器是否需要重新启动?

  • 文件权限如何工作?文件权限用户和组

  • 如何在 Vim 中启用全彩支持?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何运行 .sh 脚本?

    • 16 个回答
  • Marko Smith

    如何安装 .tar.gz(或 .tar.bz2)文件?

    • 14 个回答
  • Marko Smith

    如何列出所有已安装的软件包

    • 24 个回答
  • Marko Smith

    无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗?

    • 25 个回答
  • Martin Hope
    Flimm 如何在没有 sudo 的情况下使用 docker? 2014-06-07 00:17:43 +0800 CST
  • Martin Hope
    Ivan 如何列出所有已安装的软件包 2010-12-17 18:08:49 +0800 CST
  • Martin Hope
    La Ode Adam Saputra 无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗? 2010-11-30 18:12:48 +0800 CST
  • Martin Hope
    David Barry 如何从命令行确定目录(文件夹)的总大小? 2010-08-06 10:20:23 +0800 CST
  • Martin Hope
    jfoucher “以下软件包已被保留:”为什么以及如何解决? 2010-08-01 13:59:22 +0800 CST
  • Martin Hope
    David Ashford 如何删除 PPA? 2010-07-30 01:09:42 +0800 CST

热门标签

10.10 10.04 gnome networking server command-line package-management software-recommendation sound xorg

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve