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
    • 最新
    • 标签
主页 / server / 问题 / 738970
Accepted
Brad
Brad
Asked: 2015-11-26 09:31:35 +0800 CST2015-11-26 09:31:35 +0800 CST 2015-11-26 09:31:35 +0800 CST

使用 perl/awk/sed 前置字符串?

  • 772

我正在尝试编写对我的 apache 配置文件 (httpd.conf) 的更改的脚本。我正在尝试匹配以下字符串:

#
# DirectoryIndex: sets the file that Apache will serve if a directory

并在前面加上以下文字:

#
# Allow server status reports generated by mod_status,
# with the URL of http://servername/server-status
# Change the ".example.com" to match your domain to enable.
#
<Location /server-status>
    SetHandler server-status
    Order deny,allow
    Deny from all
    Allow from localhost ip6-localhost 127.0.0.1 192.168.0.0/255.255.255.0
</Location>

我的理解是 sed 不支持多行匹配, awk 似乎很难进行多行匹配。我试图让 perl 与 perl -0777 -pi -e 一起工作,但我似乎无法找出与原始模式匹配的正则表达式。

我更愿意将其作为一个衬里进行 - 而不是脚本,因为我希望它是可移植的(即根据需要复制和粘贴)。

有任何 perl 正则表达式专家可以帮助我设计解决方案吗?

非常感谢布拉德

编辑

以下作品:

 sed -i -e ':begin;$!N;s/#\n# DirectoryIndex/#\n# Allow server status reports generated by mod_status,\n# with the URL of http:\/\/servername\/server-status\n# Change the ".example.com" to match your domain to enable.\n#\n<Location \/server-status>\n\tSetHandler server-status\n\tOrder deny,allow\n\tDeny from all\n\tAllow from localhost ip6-localhost 192\.168\.0\.0\/255\.255\.255\.0\n<\/Location>\n\n#\n\#DirectoryIndex/;tbegin;P;‌​D' /etc/httpd/conf/httpd.conf 

但是 # 和 DirectoryIndex 之间没有空格。

但是,如果我尝试将其更改为:

sed -i -e ':begin;$!N;s/#\n# DirectoryIndex/#\n# Allow server status reports generated by mod_status,\n# with the URL of http:\/\/servername\/server-status\n# Change the ".example.com" to match your domain to enable.\n#\n<Location \/server-status>\n\tSetHandler server-status\n\tOrder deny,allow\n\tDeny from all\n\tAllow from localhost ip6-localhost 192\.168\.0\.0\/255\.255\.255\.0\n<\/Location>\n\n#\n\# DirectoryIndex/;tbegin;P;‌​D' /etc/httpd/conf/httpd.conf 

sed 命令挂起并且永远不会完成。我似乎无法弄清楚为什么?

唯一的区别是# 和 DirectoryIndex 之间有一个空格。

perl
  • 2 2 个回答
  • 820 Views

2 个回答

  • Voted
  1. Froggiz
    2015-11-26T11:15:26+08:002015-11-26T11:15:26+08:00

    你应该试试

    perl -0777 -i.original -pe 's/#\n# DirectoryIndex: sets the file that Apache will serve if a directory/#\n# DirectoryIndex: sets the file that Apache will serve if a directory\n#\n# Allow server status reports generated by mod_status,\n# with the URL of http:\/\/servername\/server-status\n# Change the ".example.com" to match your domain to enable.\n#\n<Location \/server-status>\n    SetHandler server-status\n    Order deny,allow\n    Deny from all\n    Allow from localhost ip6-localhost 127.0.0.1 192.168.0.0\/255.255.255.0\n<\/Location>/igs' /etc/httpd/conf/httpd.conf
    

    当您在正则表达式工具中使用 / 分隔符时,不要忘记转义您的字符串replace( \/for )/

    如果你通过shell运行它,你可以做一个这样的脚本

    #Set serach delimiter
    search='#\n# DirectoryIndex: sets the file that Apache will serve if a directory'
    
    #Set replace string from file
    replace=$search"\n"$(cat newConfFile)
    
    #Escape "/" char
    replace=${replace//\//\\\/ }
    
    #Launch the script
    perl -0777 -i.original -pe 's/${search}/${replace}/igs' /etc/httpd/conf/httpd.conf
    

    使用 newConfFile 包含您要添加的虚拟主机配置

    这是一个 Bash 函数,用于测试函数是否存在

    #return 0 if command exist else return 1
    canExec()
    {
    type "$1" &> /dev/null ;
    }
    

    例如canExec sed测试系统上是否存在 sed 命令

    • 2
  2. Best Answer
    TessellatingHeckler
    2015-11-26T12:28:38+08:002015-11-26T12:28:38+08:00

    使用 awk,如何:

    • 将每一行存储在“上一行”的变量中
    • 如果当前行与您要查找的第二行匹配 (DirectoryIndex),请使用上一行检查变量
    • 如果他们都匹配
      • 打印凹凸
      • 打印“当前行”
      • 打印吞下的'#'
    • 别的
      • 打印当前行
    • 用当前行更新“上一行”变量。

    这应该对您有用,因为您不需要严格地预先设置文本 - 因为您要查找的文本和要插入的文本都以您开头,#您可以保留原始#文本,插入您的文本减去第一#行中间,然后打印原始的第二行,然后打印另一#行作为您没有预先添加的行。

    你必须填写全文,但这里有足够的东西来说服我它可以工作;)

    gawk "{if (a==\"#\" && /^# DirectoryIndex/) {print \"# Allow Server\n#With the URL\n#\"; print $0} else {print $0}} {a=$0}" httpd.conf > ??
    

    (我的双引号转义适用于 Windows 的命令提示符。根据需要进行调整)。

    编辑的 bash 引用:

    gawk '{if (a=="#" && /^# DirectoryIndex/) {print "# Allow Server\n# With the URL\n#"; print $0} else {print $0}} {a=$0}' httpd.conf
    
    • 2

相关问题

  • CentOS 的依赖挑战

  • 将执行 Perl REGEX 扩展的 Lex Flex 变体

  • 将 perl 脚本作为 Windows 服务运行

  • debian 升级失败,导致 perl 问题

  • Perl 5.10 登录/注销脚本

Sidebar

Stats

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

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

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

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve