質問

I have one data file and one reg template file:

data file contain:

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 template:

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

I want to create loop which to create new reg files from the template with the name from the first colum and to change "name" located in HKEY_USERS also with the first column and to change the IP address with the second column.

For example:

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

Expected view after the command:

#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"
役に立ちましたか?

解決 2

Try:

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

A little messy since " has to be used for the shell to expand the variables in the sed-substitution and all additional " need to be escaped.

output:

$ 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"

他のヒント

sed is an excellent tool for simple substitutions on a single line, for anything else just use 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

or if you prefer:

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
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top