使用bash脚本根据用户输入编辑文件 [英] Edit a file based on user's input using bash script
问题描述
我有以下文件.dev
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 clustermode = "Standard";
9 }
10 IO-DEVICES {
11 }
12 COMPUTING-DEVICES {
13 RT_WORKSTATION FDT-C-XM-0120 = {
14 hostname = "FDT-C-XM-0120";
15 ipaddress = "fdt-c-XM-0120.fdtel.exter";
16 DISPLAYS {
17 main = "FDT-C-XM-0120:0.0";
18 }
19 SCHEDPARAM {
20 active = "0";
21 framerate = "20000";
22 subframes = "0";
23 max_consec_timeouts = "10";
24 max_total_timeouts = "1000";
25 }
26 }
27
28 RT_HOST fdt-c-agx-0008 = {
29 hostname = "fdt-c-agx-0008";
30 ipaddress = "fdt-c-agx-0008";
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 # RT_HOST fdt-c-agx-0003 = {
41 # hostname = "fdt-c-agx-0003";
42 # ipaddress = "fdt-c-agx-0003.fdtel.exter";
43 # SCHEDPARAM {
44 # active = "0";
45 # framerate = "20000";
46 # subframes = "0";
47 # max_consec_timeouts = "10";
48 # max_total_timeouts = "1000";
49 # }
50 # }
51 }
52 }
在此文件中,文本部分第1部分 (从第28行到38)
和第2部分 (从第40行到50)
是用户在其间切换的部分.如我们所见,第2部分已被注释掉,第1部分处于活动状态.
In this file the text parts part 1 (from line 28 till 38)
and part 2 (from line 40 till 50)
are parts which the user switch between. As we can see part 2 is commented out and part one is active.
因此,我正在尝试使用bash脚本自动执行该操作,以使用户仅输入他想要的部件号,而另一个则被注释掉.这样一来,使用中就不必注释掉每一行.
So i'm trying to automate that using bash script in such a way that the user only enters the part number he wants and the other is commented out. This way the use must not comment out each line.
# example
if [ "$userEntry" = "part2"]
then
deactivate part one by typing adding from line 28 till 38 and activate part 2 by removing the #
和输出看起来像
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 clustermode = "Standard";
9 }
10 IO-DEVICES {
11 }
12 COMPUTING-DEVICES {
13 RT_WORKSTATION FDT-C-XM-0120 = {
14 hostname = "FDT-C-XM-0120";
15 ipaddress = "fdt-c-XM-0120.fdtel.exter";
16 DISPLAYS {
17 main = "FDT-C-XM-0120:0.0";
18 }
19 SCHEDPARAM {
20 active = "0";
21 framerate = "20000";
22 subframes = "0";
23 max_consec_timeouts = "10";
24 max_total_timeouts = "1000";
25 }
26 }
27
28 # RT_HOST fdt-c-agx-0008 = {
29 # hostname = "fdt-c-agx-0008";
30 # ipaddress = "fdt-c-agx-0008";
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 RT_HOST fdt-c-agx-0003 = {
41 hostname = "fdt-c-agx-0003";
42 ipaddress = "fdt-c-agx-0003.fdtel.exter";
43 SCHEDPARAM {
44 active = "0";
45 framerate = "20000";
46 subframes = "0";
47 max_consec_timeouts = "10";
48 max_total_timeouts = "1000";
49 }
50 }
51 }
52 }
请注意,file.dev中的行顺序不变.
Note that the lines order in the file.dev do not change.
我希望我能澄清我的问题,并预先感谢
I hope i could make my question clear and thanks in advance
推荐答案
带有 ed
(如果有/可接受).
With ed
if it is available/acceptable.
#!/usr/bin/env bash
if [[ "$userEntry" == "part2" ]]; then
printf '%s\n' '40,50s/^[[:blank:]]*#//' '28,38s/^/#/' ,p Q |
ed -s file.txt
fi
将仅将新输出打印到 stdout
,但不会更改/编辑文件.如果需要就地编辑,请将 Q
更改为 w
.删除,p
使输出静音.
Will just print the new output to stdout
but the file will not be change/edited. Change Q
to w
if in-place editing is needed. Remove the ,p
to silence the output.
使用 sed
sed '40,50s/^[[:blank:]]*#//;28,38s/^/#/' file.txt
请注意,如果需要进行就地编辑,则使用 -i
标志时,不同的 sed
版本具有不同的语法.
Note that different sed
version has different syntax when using the -i
flag if in-place editing is needed.
按照OP的解释.
#!/usr/bin/env bash
part1=28
part2=40
if [[ "$userEntry" == "part2" ]]; then
if [[ $(grep -nm1 \# file.txt | cut -d':' -f1) == "$part2" ]]; then
sed '40,50s/^[[:blank:]]*#*//;28,38s/^/#/' file.txt
else
sed '28,38s/^/#/' file.txt
fi
elif [[ "$userEntry" == "part1" ]]; then
if [[ $(grep -nm1 \# file.txt | cut -d':' -f1) == "$part1" ]]; then
sed '28,38s/^[[:blank:]]*#*//;40,50s/^/#/' file.txt
else
sed '40,50s/^/#/' file.txt
fi
fi
返回GNU grep(1)
这篇关于使用bash脚本根据用户输入编辑文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!