【问题标题】:How to create multiple reg files using linux regex如何使用 linux regex 创建多个 reg 文件
【发布时间】:2013-03-17 09:25:28
【问题描述】:

我有一个数据文件和一个reg模板文件:

数据文件包含:

c01218 172.20.13.50
c01203 172.20.13.35
c01204 172.20.13.36
c01220 172.20.13.52
c01230 172.20.13.55

注册模板:

[HKEY_USERS\S-1-5-21-2000478354-2111687655-1801674531-230160\Software\SimonTatham\PuTTY\Sessions\name]
"Present"=dword:00000001
"HostName"="172.28.130.0"

我想创建循环,从模板创建新的 reg 文件,名称来自第一列,并更改位于 HKEY_USERS 中的“名称”也与第一列,并使用第二列更改 IP 地址。

例如:

sed -e "s/name/name1/g" -e "s/172.28.130.0/172.28.130.1/g" 1.reg

命令后的预期视图:

#cat c01218.reg

[HKEY_USERS\S-1-5-21-2000478354-2111687655-1801674531-230160\Software\SimonTatham\PuTTY\Sessions\c01218]
"Present"=dword:00000001
"HostName"="172.20.13.50"

【问题讨论】:

    标签: sed awk grep tr


    【解决方案1】:

    sed 是用于在单行上进行简单替换的出色工具,对于其他任何内容,只需使用 awk:

    awk '{ printf "[HKEY_USERS\\S-1-5-21-2000478354-2111687655-1801674531-230160\\Software\\SimonTatham\\PuTTY\\Sessions\\name]\n\"Present\"=dword:00000001\n\"HostName\"=\"%s\"\n", $2 > $1 }' data
    

    或者如果您愿意:

    awk -v template="\
    [HKEY_USERS\\S-1-5-21-2000478354-2111687655-1801674531-230160\\Software\\SimonTatham\\PuTTY\\Sessions\\name]
    \"Present\"=dword:00000001
    \"HostName\"=\"%s\"
    " '{ printf template, $2 > $1 }' data
    

    【讨论】:

      【解决方案2】:

      试试:

      $ while read a b; do sed "s/^\"HostName.*$/\"HostName\"=\"$b\"/" template > $a; done < data
      

      有点混乱,因为 shell 必须使用 " 来扩展 sed-substitution 中的变量,并且所有额外的 " 都需要转义。

      输出:

      $ ls
      c01203  c01204  c01218  c01220  c01230  data  template
      
      $ cat c*
      [HKEY_USERS\S-1-5-21-2000478354-2111687655-1801674531-230160\Software\SimonTatham\PuTTY\Sessions\name]
      "Present"=dword:00000001
      "HostName"="172.20.13.35"
      [HKEY_USERS\S-1-5-21-2000478354-2111687655-1801674531-230160\Software\SimonTatham\PuTTY\Sessions\name]
      "Present"=dword:00000001
      "HostName"="172.20.13.36"
      [HKEY_USERS\S-1-5-21-2000478354-2111687655-1801674531-230160\Software\SimonTatham\PuTTY\Sessions\name]
      "Present"=dword:00000001
      "HostName"="172.20.13.50"
      [HKEY_USERS\S-1-5-21-2000478354-2111687655-1801674531-230160\Software\SimonTatham\PuTTY\Sessions\name]
      "Present"=dword:00000001
      "HostName"="172.20.13.52"
      [HKEY_USERS\S-1-5-21-2000478354-2111687655-1801674531-230160\Software\SimonTatham\PuTTY\Sessions\name]
      "Present"=dword:00000001
      "HostName"="172.20.13.55"
      

      【讨论】:

      • 谢谢你帮了我很多对我有用的正确sintax是:while read a b;做 sed -e "s/name/$a/g" -e "s/^\"HostName.*$/\"HostName\"=\"$b\"/" template.reg > $a.reg;完成
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-19
      相关资源
      最近更新 更多