如何通过 bash 脚本访问 .dev 文件
How to access .dev file via bash script
下面是一个 device.dev
文件,其中包含名为 ads2 的特定应用程序的一些配置信息。
1 DEVICES {
2 GLOBAL-CONFIG {
3 framerate = "20000";
4 subframes = "0";
5 max_consec_timeouts = "10";
6 max_total_timeouts = "1000";
7 schedmode = "Standard";
8 }
9 IO-DEVICES {
10 }
11 COMPUTING-DEVICES {
12 RT_WORKSTATION x-NB-0144 = {
13 hostname = "x-x-0144";
14 ipaddress = "xxx.x.x.xx";
15 DISPLAYS {
16 main = "FDT-C-VM-0094:0.0";
17 }
18 SCHEDPARAM {
19 active = "0";
20 framerate = "20000";
21 subframes = "0";
22 max_consec_timeouts = "10";
23 max_total_timeouts = "1000";
24 }
25 }
27
28 RT_HOST xxx-c-agx-vw-89 = {
29 hostname = "xxxxx@xxxx-desktop";
30 ipaddress = "xx.xx.xx.xx";
31 SCHEDPARAM {
32 active = "0";
33 framerate = "20000";
34 subframes = "0";
35 max_consec_timeouts = "10";
36 max_total_timeouts = "1000";
37 }
38 }
39 }
40 }
我正在尝试编写一个 bash 脚本,该脚本接受 IP 地址输入,然后访问 device.dev
文件并将其传递给变量 ipaddress
第 30 行。
那么是否可以通过 bash 脚本访问 devices.dev 文件?
提前致谢
假设您想要精确地替换第 30 行的值,您可以在 device.dev
的同一目录中创建此文件 sed.sh
#!/bin/bash
IP=
sed -i "30s/ipaddress.*/ipaddress = \"$IP\"/" device.dev
并执行
bash sed.sh 1.2.3.4
device.dev 将在第 30 行有 1.2.3.4
使用 GNU awk 并仅关注第二个条目:
awk -v ipadd="1.1.1.1" '/ipaddress/ { cnt++ } /ipaddress/ && cnt==2 { lne=gensub(/(^.*\")(.*)(\".*$)/,"\1"ipadd"\3",[=10=]);print lne;next }1' devices.dev > devices.tmp && mv -f devices.tmp devices.dev
解释:
awk -v ipadd="1.1.1.1" '/ipaddress/ { # Pass the ip address to change to as a variable ipadd to awk
cnt++ # Where there is ipaddress in the line, increment a cnt variable
}
/ipaddress/ && cnt==2 { # Where there is ipaddress in the line and cnt is 2, process
lne=gensub(/(^.*\")(.*)(\".*$)/,"\1"ipadd"\3",[=11=]); # Set a variable lne to an entry that substitutes the existing IP address for the one passed in ipadd using awk's gensub function
print lne; # Print the amendment
next # Skip to the next line
}1' devices.dev > devices.tmp && mv -f devices.tmp devices.dev # Print all none amended lines and redirect the output to a temp file (devices.tmp) before over writing the devices.dev file with the temp file
下面是一个 device.dev
文件,其中包含名为 ads2 的特定应用程序的一些配置信息。
1 DEVICES {
2 GLOBAL-CONFIG {
3 framerate = "20000";
4 subframes = "0";
5 max_consec_timeouts = "10";
6 max_total_timeouts = "1000";
7 schedmode = "Standard";
8 }
9 IO-DEVICES {
10 }
11 COMPUTING-DEVICES {
12 RT_WORKSTATION x-NB-0144 = {
13 hostname = "x-x-0144";
14 ipaddress = "xxx.x.x.xx";
15 DISPLAYS {
16 main = "FDT-C-VM-0094:0.0";
17 }
18 SCHEDPARAM {
19 active = "0";
20 framerate = "20000";
21 subframes = "0";
22 max_consec_timeouts = "10";
23 max_total_timeouts = "1000";
24 }
25 }
27
28 RT_HOST xxx-c-agx-vw-89 = {
29 hostname = "xxxxx@xxxx-desktop";
30 ipaddress = "xx.xx.xx.xx";
31 SCHEDPARAM {
32 active = "0";
33 framerate = "20000";
34 subframes = "0";
35 max_consec_timeouts = "10";
36 max_total_timeouts = "1000";
37 }
38 }
39 }
40 }
我正在尝试编写一个 bash 脚本,该脚本接受 IP 地址输入,然后访问 device.dev
文件并将其传递给变量 ipaddress
第 30 行。
那么是否可以通过 bash 脚本访问 devices.dev 文件?
提前致谢
假设您想要精确地替换第 30 行的值,您可以在 device.dev
的同一目录中创建此文件 sed.sh#!/bin/bash
IP=
sed -i "30s/ipaddress.*/ipaddress = \"$IP\"/" device.dev
并执行
bash sed.sh 1.2.3.4
device.dev 将在第 30 行有 1.2.3.4
使用 GNU awk 并仅关注第二个条目:
awk -v ipadd="1.1.1.1" '/ipaddress/ { cnt++ } /ipaddress/ && cnt==2 { lne=gensub(/(^.*\")(.*)(\".*$)/,"\1"ipadd"\3",[=10=]);print lne;next }1' devices.dev > devices.tmp && mv -f devices.tmp devices.dev
解释:
awk -v ipadd="1.1.1.1" '/ipaddress/ { # Pass the ip address to change to as a variable ipadd to awk
cnt++ # Where there is ipaddress in the line, increment a cnt variable
}
/ipaddress/ && cnt==2 { # Where there is ipaddress in the line and cnt is 2, process
lne=gensub(/(^.*\")(.*)(\".*$)/,"\1"ipadd"\3",[=11=]); # Set a variable lne to an entry that substitutes the existing IP address for the one passed in ipadd using awk's gensub function
print lne; # Print the amendment
next # Skip to the next line
}1' devices.dev > devices.tmp && mv -f devices.tmp devices.dev # Print all none amended lines and redirect the output to a temp file (devices.tmp) before over writing the devices.dev file with the temp file