【问题标题】:Edit a file based on user's input using bash script使用 bash 脚本根据用户的输入编辑文件
【发布时间】:2021-05-02 17:15:02
【问题描述】:

我有以下文件.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        }

在此文件中,文本部分 part 1 (from line 28 till 38)part 2 (from line 40 till 50) 是用户在其中切换的部分。正如我们所见,第 2 部分已被注释掉,第 1 部分处于活动状态。

所以我正在尝试使用 bash 脚本自动执行该操作,使用户只输入他想要的部件号,而另一个被注释掉。这样用户就不能注释掉每一行。

# 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 中的行顺序不会改变。

我希望我能把我的问题说清楚并提前感谢

【问题讨论】:

标签: bash shell command-line kernel


【解决方案1】:

ed 如果可用/可接受。

#!/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 以使输出静音。


sed

sed '40,50s/^[[:blank:]]*#//;28,38s/^/#/' file.txt

请注意,如果需要就地编辑,则在使用-i 标志时,不同的sed 版本具有不同的语法。


根据 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)

【讨论】:

  • 非常感谢。我试过你的代码。它返回以下错误 test.sh:line 9: ed: command not found。是的,更改必须保存在文件中
  • 好吧,ed 没有安装,所以使用sed-i 标志/选项来编辑文件。
  • 当应用带有标志 -i 的 sed 时,它会正确写入 # 但是,在问题中这意味着如果用户输入第 1 部分,则必须在第 1 部分中删除 # (以防它被注释掉)和因此成为活动代码,第 2 部分应使用 # 注释,因此变为非活动代码,反之亦然
  • 我已经发布了一个新的解决方案,试试看,不过我没有添加-i 标志。
  • 完美。你真的是一座金矿。非常感谢
猜你喜欢
  • 2015-07-06
  • 2021-12-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-08
  • 2022-10-22
  • 2021-03-23
  • 2010-12-05
相关资源
最近更新 更多