我有一个数据文件和一个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
reg模板:
[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中的“name”,并使用第二列更改IP地址。 / p>
例如:
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"
答案 0 :(得分:2)
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
答案 1 :(得分:1)
尝试:
$ while read a b; do sed "s/^\"HostName.*$/\"HostName\"=\"$b\"/" template > $a; done < data
因为"
必须使用shell来扩展sed替换中的变量而且需要转义所有其他"
,所以有点麻烦。
输出:
$ 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"